library(readr)
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
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
# install RColorBrewer
# install.packages("RColorBrewer")

# load required packages
library(RColorBrewer)
setwd("~/Downloads")
nations <- read_csv("nations.csv") %>%
  mutate(gdp_tn = gdp_percap*population/10^12)
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   iso2c = col_character(),
##   iso3c = col_character(),
##   country = col_character(),
##   year = col_double(),
##   gdp_percap = col_double(),
##   population = col_double(),
##   birth_rate = col_double(),
##   neonat_mortal_rate = col_double(),
##   region = col_character(),
##   income = col_character()
## )
# prepare data
big4 <- nations %>%
  filter(iso3c == "KOR" | iso3c == "FIN" | iso3c == "IND" | iso3c == "IDN") %>%
  arrange(year)
# customize the tooltips
big4 %>%
  rename(Country = iso3c) %>%
  ggplot(aes(year, gdp_tn, color = Country)) +
  geom_line(size = 0.5) +
  geom_point(show.legend = FALSE, alpha = 0.2, size = 3)  + 
  scale_color_brewer(palette = "Set1") +
  ylab("GDP ($ trillion)") +
  xlab("Year") +
  ggtitle("India's Rise to Become the Largest Economy")

big4
## # A tibble: 100 x 11
##    iso2c iso3c country    year gdp_percap population birth_rate neonat_mortal_r…
##    <chr> <chr> <chr>     <dbl>      <dbl>      <dbl>      <dbl>            <dbl>
##  1 FI    FIN   Finland    1990     17906.    4986431       13.1              3.9
##  2 ID    IDN   Indonesia  1990      2894.  181436821       25.8             30.3
##  3 IN    IND   India      1990      1146.  870601776       31.5             57.4
##  4 KR    KOR   Korea, R…  1990      8436.   42869283       15.2              2.7
##  5 FI    FIN   Finland    1991     17313.    5013740       13                3.7
##  6 ID    IDN   Indonesia  1991      3201.  184614740       25.2             29.6
##  7 IN    IND   India      1991      1173.  888513869       30.9             56.1
##  8 KR    KOR   Korea, R…  1991      9533.   43295704       16.4              2.5
##  9 FI    FIN   Finland    1992     17023.    5041992       13.2              3.6
## 10 ID    IDN   Indonesia  1992      3452.  187762097       24.6             28.9
## # … with 90 more rows, and 3 more variables: region <chr>, income <chr>,
## #   gdp_tn <dbl>
nations <- read_csv("nations.csv") %>%
  mutate(gdp_tn = gdp_percap*population/10^12)
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   iso2c = col_character(),
##   iso3c = col_character(),
##   country = col_character(),
##   year = col_double(),
##   gdp_percap = col_double(),
##   population = col_double(),
##   birth_rate = col_double(),
##   neonat_mortal_rate = col_double(),
##   region = col_character(),
##   income = col_character()
## )
regions <- nations %>%
  group_by(region, year) %>%
  summarize(GDP = sum(gdp_tn, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.
regions %>%
  ggplot(aes(year, GDP, group = region, type = "area")) +
  geom_area(mapping = aes(fill = region), color = 'white') + 
  scale_fill_brewer(palette = "Set2") +
  ylab("GDP ($ trillion)") +
  xlab("Year") +
  ggtitle("GDP by World Bank Region")

regions
## # A tibble: 175 x 3
## # Groups:   region [7]
##    region                      year   GDP
##    <chr>                      <dbl> <dbl>
##  1 East Asia & Pacific         1990 5.52 
##  2 Europe & Central Asia       1990 9.36 
##  3 Latin America & Caribbean   1990 2.40 
##  4 Middle East & North Africa  1990 1.66 
##  5 North America               1990 6.54 
##  6 South Asia                  1990 1.35 
##  7 Sub-Saharan Africa          1990 0.787
##  8 East Asia & Pacific         1991 6.03 
##  9 Europe & Central Asia       1991 9.71 
## 10 Latin America & Caribbean   1991 2.55 
## # … with 165 more rows