Call up the needed packages: dplyr, ggplot2, tidyverse, RColorBrewer.
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(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 3.0.1 ✓ purrr 0.3.4
## ✓ tidyr 1.1.0 ✓ 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)
Load in the dataset and create a new variable in the data, using mutate from dplyr, giving the GDP of each country in trillions of dollars, by multiplying gdp_percap by population and dividing by a trillion.
nations <- read_csv("nations.csv") %>%
mutate(gdp_tn = gdp_percap*population/1000000000000)
## Parsed with 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()
## )
Plot 1
Filter the data with dplyr for the four desired countries.
big4 <- nations %>%
filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA") %>%
arrange(year)
Create chart using ggplot2. Add both geom_point and geom_line layers, and use the Set1 ColorBrewer palette using: scale_color_brewer(palette = “Set1”).
plot1 <- big4 %>%
ggplot(aes(year, gdp_tn, color = country)) +
geom_point(size = 2) +
xlab("Year") +
ylab("GDP ($ Trillion)") +
geom_line() +
scale_color_brewer(palette = "Set1") +
labs(fill= "Country") +
ggtitle("China's Rise to Become the Largest Economy")
plot1

Plot 2
Use the group_by command for region and year, and then summarize on the mutated value for gdp_percap using summarise(sum = sum(gdp_percap, na.rm = TRUE)). We use na.rm = TRUE because there are NAs in this data.
regions <- nations %>%
group_by(year,region) %>%
summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) %>%
arrange(year,region)
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
Use ggplot2 and geom_area to create an area graph based on region. Use the Set2 ColorBrewer palette using scale_fill_brewer(palette = “Set2”) and put a thin white line around each area using geom_area(size = .5, color = “white”).
plot2 <- regions %>%
ggplot(aes(year, gdp_tn, fill = region)) +
geom_area(size = .5, color = "white") +
xlab("Year") +
ylab("GDP ($ Trillion)") +
scale_fill_brewer(palette = "Set2") +
labs(fill= "Region") +
ggtitle("GDP by World Bank Region")
plot2
