Week 9 Class Work

Author

Emma Wang

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(dplyr)
library(ggplot2)
nation = read_csv("~/Downloads/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(nation)
# 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>
nation_GDP = nation |> 
  mutate('Country GDP' = gdp_percap * population / 1000000000000)
head(nation_GDP)
# A tibble: 6 × 11
  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
# ℹ 3 more variables: region <chr>, income <chr>, `Country GDP` <dbl>
select_nations = nation_GDP |> 
  filter(country %in% c("China", "Germany", "Japan", "United States"))
select_nations
# A tibble: 100 × 11
   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
   <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
 1 CN    CHN   China    1992      1260. 1164970000       18.3               29.4
 2 CN    CHN   China    2005      5053. 1303720000       12.4               14  
 3 CN    CHN   China    2000      2915. 1262645000       14.0               21.2
 4 CN    CHN   China    1991      1091. 1150780000       19.7               29.7
 5 CN    CHN   China    2013     12219. 1357380000       12.1                6.3
 6 CN    CHN   China    1999      2650. 1252735000       14.6               22.2
 7 CN    CHN   China    2014     13255. 1364270000       12.4                5.9
 8 CN    CHN   China    2003      3934. 1288400000       12.4               17.1
 9 CN    CHN   China    2004      4423. 1296075000       12.3               15.5
10 CN    CHN   China    1993      1453. 1178440000       18.1               28.8
# ℹ 90 more rows
# ℹ 3 more variables: region <chr>, income <chr>, `Country GDP` <dbl>

Chart 1

ggplot(select_nations, aes(x = year, y = `Country GDP`, group = country, color = country)) +
  geom_line() +
  geom_point(size = .8) +
  theme_minimal() +
  scale_color_brewer(palette = "Set1") +
  labs( 
    title = "China's Rise to Become the Largest Economy", 
    y = "GDP ($ trillions", 
    color = "")

Chart 2

GDP = nation |> 
  mutate('Country GDP' = gdp_percap * population / 1000000000000)
GDP
# A tibble: 5,275 × 11
   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
 7 AD    AND   Andorra  2004         NA      78337       10.9                2  
 8 AD    AND   Andorra  2010         NA      84419        9.8                1.7
 9 AD    AND   Andorra  2001         NA      67770       11.8                2.1
10 AD    AND   Andorra  2002         NA      71046       11.2                2.1
# ℹ 5,265 more rows
# ℹ 3 more variables: region <chr>, income <chr>, `Country GDP` <dbl>
group_nation = GDP |> 
  group_by(region, year) |> 
  summarise(Country_GDP = sum(`Country 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.
group_nation
# A tibble: 175 × 3
# Groups:   region [7]
   region               year Country_GDP
   <chr>               <dbl>       <dbl>
 1 East Asia & Pacific  1990        5.52
 2 East Asia & Pacific  1991        6.03
 3 East Asia & Pacific  1992        6.50
 4 East Asia & Pacific  1993        7.04
 5 East Asia & Pacific  1994        7.64
 6 East Asia & Pacific  1995        8.29
 7 East Asia & Pacific  1996        8.96
 8 East Asia & Pacific  1997        9.55
 9 East Asia & Pacific  1998        9.60
10 East Asia & Pacific  1999       10.1 
# ℹ 165 more rows
ggplot(group_nation, aes(x = year, y = Country_GDP, fill = region)) +
  geom_area() +
  theme_minimal() +
  labs(
    title = "GDP by World Bank Region", 
    y = "GDP ($ trillions" 
  ) +
  scale_fill_brewer(palette = "Set2")