Joyce Fang Assignment 6

Set Up

Importing

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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
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.
view(nations)

Data Cleaning

nations <- nations %>% mutate(
  gdp = (gdp_percap * population/1000000000000)
) 

First Chart

nations1 <- nations%>% filter(
  country == "China" | country == "Germany" | country == "Japan" | country == "United States"
) 

nations1 %>% ggplot(aes(x = year, y = gdp, color = country)) + 
  geom_point() + 
  geom_line() + 
  scale_color_brewer(palette = "Set1") + 
  labs(
    x = "year",
    y = "GDP ($trillion)",
    title = "China's Rise to Become the Largest Economy"
  )

Second Chart

nations2 <- nations %>% 
  group_by(region, year) %>% 
  summarize(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
nations2 %>% ggplot(aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white") + 
  scale_fill_brewer(palette = "Set2") + 
  labs(
    x = "year",
    y = "GDP ($trillion)",
    title = "GDP by World Bank Region"
  )