library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
## 
## 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)
## Warning: package 'ggplot2' was built under R version 3.6.3
df <- read.csv("nations.csv")
glimpse(df)
## Rows: 5,275
## Columns: 10
## $ iso2c              <fct> AD, AD, AD, AD, AD, AD, AD, AD, AD, AD, AD, AD, ...
## $ iso3c              <fct> AND, AND, AND, AND, AND, AND, AND, AND, AND, AND...
## $ country            <fct> Andorra, Andorra, Andorra, Andorra, Andorra, And...
## $ year               <int> 1996, 1994, 2003, 1990, 2009, 2011, 2004, 2010, ...
## $ gdp_percap         <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ population         <dbl> 64291, 62707, 74783, 54511, 85474, 82326, 78337,...
## $ birth_rate         <dbl> 10.900, 10.900, 10.300, 11.900, 9.900, NA, 10.90...
## $ neonat_mortal_rate <dbl> 2.8, 3.2, 2.0, 4.3, 1.7, 1.6, 2.0, 1.7, 2.1, 2.1...
## $ region             <fct> Europe & Central Asia, Europe & Central Asia, Eu...
## $ income             <fct> High income, High income, High income, High inco...
df <- df %>% mutate(gdp_country = gdp_percap*population/10^12)
fig1 <- df %>% filter(country == "China" | country == "Germany" | country == "Japan" | country == "United States" ) %>%
  ggplot(aes(year, gdp_country, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1")+
  ggtitle("China's Rise to Become the Largest Economy") +
  ylab("GDP ($ trillion)") +
  theme(legend.title = element_blank())
fig1

fig2 <- df %>% group_by(region, year) %>%
  summarize(sum = sum(gdp_country, na.rm = TRUE)) %>%
  ggplot(aes(year, sum, fill = region)) +
  geom_area(color = "white") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by World Bank Region",
       y = "GDP ($ trillion)") 
## `summarise()` regrouping output by 'region' (override with `.groups` argument)
fig2