Untitled

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(ggplot2)
library(dplyr) 
library(readr)
list.files() 
[1] ":Users:terrymensah:Documents:Data 101"            
[2] "Assignment 6.qmd"                                 
[3] "Assignment 6.rmarkdown"                           
[4] "Assignment-6.rmarkdown"                           
[5] "Hausman_ARS_FY2025_LESA-STU_FINAL Highlighted.csv"
[6] "NYPD_Hate_Crimes.csv"                             
[7] "Project 1.html"                                   
[8] "Project 1.qmd"                                    
[9] "rsconnect"                                        
library(dplyr)
library(ggplot2)
library(readr)

# Set the correct working directory
setwd("~/Documents/Data 101/:Users:terrymensah:Documents:Data 101")

# Import the Nations dataset
nations <- read_csv("nations.csv", skip = 1)
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Check column names
colnames(nations)
 [1] "iso2c"              "iso3c"              "country"           
 [4] "year"               "gdp_percap"         "population"        
 [7] "birth_rate"         "neonat_mortal_rate" "region"            
[10] "income"            
# Filter for four countries
nations1 <- nations |>
  filter(country %in% c("United States",
                        "Canada",
                        "Mexico",
                        "Brazil"))

# Create the scatterplot
p1 <- nations1 |>
  ggplot(aes(x = year, y = birth_rate,
             color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "Birth Rates by Country",
    x = "Year",
    y = "Birth Rate",
    color = "Country"
  )

p1

nations2 <- nations |>
  mutate(gdp = gdp_percap * population) |>
  group_by(region, year) |>
  summarise(
    GDP = sum(gdp, na.rm = TRUE),
    .groups = "drop"
  ) 
p2 <- nations2 |>
  ggplot(aes(x = year, y = GDP / 1e12,
             color = region)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set2") +
  labs(
    title = "GDP by Region Over Time",
    x = "Year",
    y = "Total GDP (Trillions)",
    color = "Region"
  ) 

p2

setwd("~/Documents/Data 101/:Users:terrymensah:Documents:Data 101")