Download the packages

#install.packages("ggplot2")
#install.packages("dbplyr")
#install.packages("plotly")
#install.packages("RColorBrewer")

Call the packages

library(ggplot2)
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
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(RColorBrewer)

Load the nations dataset

setwd("C:/Users/User/Documents/Data_Science/datasets")

nations <- read.csv("nations.csv") %>%
  mutate(gdp = (gdp_percap * population) / 10^12)

Look at a preview of the dataset

head(nations)
##   iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
## 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.0
## 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
##                  region      income gdp
## 1 Europe & Central Asia High income  NA
## 2 Europe & Central Asia High income  NA
## 3 Europe & Central Asia High income  NA
## 4 Europe & Central Asia High income  NA
## 5 Europe & Central Asia High income  NA
## 6 Europe & Central Asia High income  NA

Filter desired countries

big4 <- nations %>%
  filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA") %>%
  arrange(year)

Make the ggplot

country_gdp_top4 <- big4 %>%
  ggplot(aes(y=gdp, x=year, color=country)) +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  theme_minimal() +
  ggtitle("China's Rise to Become the Largest Economy") +
  xlab("Year") +
  ylab("GDP ($ trillion)")
country_gdp_top4 <- ggplotly(country_gdp_top4)
country_gdp_top4

Group the dataset by region

regions <- nations %>%
  group_by(year,region) %>%
  summarize(gdp = sum(gdp, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.

Make the ggplot

country_regions <- ggplot(regions, aes(x=year,y = gdp, fill=region), color="white", lwd=2) + 
  geom_area(alpha=1, size=0.5, colour="white") +
  scale_fill_brewer(palette = "Set2") +
  xlab("Year") +
  ylab("GDP (trillions)")+
  ggtitle("GDP by World Bank Region") +
  theme_light()+
  theme(plot.title = element_text(hjust = 0.5))

country_regions