Nations Assignment

Author

M Sullivan

Load the libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.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(tinytex)
library(dplyr)
library(RColorBrewer)

setwd("C:/Users/micha/OneDrive/Documents/DATA 110")
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>

Add new column for GDP in Trillions

nations <-nations |>
  mutate(gdp_tn = gdp_percap*population/10^12)

Filter out for only four desired countries

big4 <-nations |>
  filter(country %in% c("China", "Germany", "Japan", "United States"))
head(big4)
# 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 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
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tn <dbl>

#Plot GDP of four countries

ggplot(big4,
       aes(x = year, y = gdp_tn, color = country)) +
  geom_point() +
  geom_line()+
  xlab("Year") +
  ylab("GDP in Trillions") +
  ggtitle("China's Rise to Become the Largest Economy") +
  scale_fill_brewer(palette = "Set1") +
  theme(axis.text.x = element_text(angle = 50, hjust = 1))

Group country by year

all_nations <-nations |>
  group_by(region,year) |>
  summarise(
    gdp_tn = sum(gdp_tn,na.rm = TRUE),
    .groups = 'drop'
  )

Plot Area Plot

ggplot(all_nations,
       aes(x = year, y = gdp_tn, fill = region)) +
  geom_area(color ="white", size = 0.4) +
  xlab("Year") +
  ylab("GDP ($ trillion") +
  ggtitle("GDP by World Bank Region") +
  scale_fill_brewer(palette = "Set2") +
  theme(axis.text.x = element_text(angle = 50, hjust = 1))
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.