Load Data

setwd("~/Desktop/R Datasets/RSaidi CSV Datasets")
nations<-read.csv("nations.csv")

Import Relevant Libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.1
## ✓ tidyr   1.1.1     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(RColorBrewer)
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

Plot 1

nations1 <- nations %>% mutate(gdp = (gdp_percap*population)/1000000000000)

nations2 <- nations1 %>% filter(country == "China" | country == "Germany" | country == "Japan" | country == "United States") 
head(nations2)
##   iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
## 1    CN   CHN   China 1992   1260.162 1164970000      18.27               29.4
## 2    CN   CHN   China 2005   5053.379 1303720000      12.40               14.0
## 3    CN   CHN   China 2000   2915.415 1262645000      14.03               21.2
## 4    CN   CHN   China 1991   1091.449 1150780000      19.68               29.7
## 5    CN   CHN   China 2013  12218.521 1357380000      12.08                6.3
## 6    CN   CHN   China 1999   2649.745 1252735000      14.64               22.2
##                region              income       gdp
## 1 East Asia & Pacific Upper middle income  1.468052
## 2 East Asia & Pacific Upper middle income  6.588191
## 3 East Asia & Pacific Upper middle income  3.681134
## 4 East Asia & Pacific Upper middle income  1.256017
## 5 East Asia & Pacific Upper middle income 16.585176
## 6 East Asia & Pacific Upper middle income  3.319429
nations_plot <- ggplot(nations2, aes(year,gdp,color = country)) + geom_point() + geom_line() + ylab("GDP ($ trillion)") + xlab("Year") + ggtitle("China's Rise to Become the World's Largest Economy") + theme(plot.title = element_text(hjust = 0.5))

nations_plot1 <- nations_plot+scale_color_brewer(palette = "Set1") + theme(legend.title = element_blank())

nations_plot1 <- ggplotly(nations_plot1)
nations_plot1

Plot 2

nations3 <- nations1 %>% group_by(region,year) %>% summarise(GDP = sum(gdp,na.rm = TRUE))
## `summarise()` regrouping output by 'region' (override with `.groups` argument)
nations_plot2 <- ggplot(nations3, aes(year,GDP,fill = region)) + geom_area(color = "white")+ scale_fill_brewer(palette = "Set2")+ ylab("GDP ($ trillion)") + xlab("Year") + ggtitle("GDP by Region") + theme(legend.title = element_blank()) + theme(plot.title = element_text(hjust = 0.5))

nations_plot3 <- ggplotly(nations_plot2)
nations_plot3