library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(readr)
library(RColorBrewer)
setwd("~/Library/CloudStorage/OneDrive-montgomerycollege.edu/DATA 110 - Mais Alraee/week 6 3:3 - 3:9")
nations <- read_csv("nations.csv")
## 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.
head(nations)
## # A tibble: 6 × 10
##   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
##   <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
## 1 AD    AND   Andorra  1996         NA      64291       10.9                2.8
## 2 AD    AND   Andorra  1994         NA      62707       10.9                3.2
## 3 AD    AND   Andorra  2003         NA      74783       10.3                2  
## 4 AD    AND   Andorra  1990         NA      54511       11.9                4.3
## 5 AD    AND   Andorra  2009         NA      85474        9.9                1.7
## 6 AD    AND   Andorra  2011         NA      82326       NA                  1.6
## # ℹ 2 more variables: region <chr>, income <chr>
nations <- nations |> 
  mutate(gdp_trillions = (gdp_percap * population) / 1e12)
countries <- c("United States", "China", "Japan", "Germany")

nations_clean <- nations |>
  filter(country %in% countries)
chart1 <- ggplot(nations_clean, aes(x = year, y = gdp_trillions, color = country)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  scale_color_brewer(palette = "Set1") +
  labs(title = "China's Rise to Become the Largest Economy", 
       x = "Year", 
       y = "GDP (Trillions of $)", 
       color = "Country") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
chart1

nations_region <- nations |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp_trillions, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
chart2 <- ggplot(nations_region, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", size = 0.2) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by World Bank Region", 
       x = "Year", 
       y = "GDP ($ Trillions) ", 
       fill = "Region") +
  theme_bw()

chart2