pacman::p_load(ggplot2, dplyr, plotly, readr) #load packages
nations <- read_csv("C:/Users/Owner/Desktop/My Documents/School/DataFiles/nations.csv") # read nations dataset
##
## -- 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()
## )
nations <- mutate(nations, gdp = (gdp_percap * population) / 10^12) # calculate gdp in trillions
Charts of Nations dataset
Point/Line charts
c4 <- filter(nations, country == "China" | country == "Germany" | country == "Japan" | country == "United States")
# create line & point plot, add region for interactive tool tip
c4_chart <- ggplot(c4, aes(x = year, y = gdp, color = country, text = paste("region:", region))) +
labs(title = "China's Rise to Become the Largest Economy") +
ylab("GDP ($ trillion)") +
xlab("Year") +
theme_light(base_size = 12) + # set theme & font size
geom_line() +
geom_point() +
scale_color_brewer(name = "Country", palette = "Set1")
c4_chart # show static plot

Interactive Point/Line chart
ggplotly(c4_chart) # show interactive plot
Area plots
ap <- nations %>%
group_by(region, year) %>% # group nations by region & year
summarise(GDP = sum(gdp, na.rm = TRUE)) %>% # create total GDP by region & year
ggplot(aes(x = year, y = GDP, fill = region) ) + # create area plot
labs(title = "GDP by World Bank Region") +
ylab("GDP ($ trillion)") +
xlab("Year") +
theme_minimal(base_size = 12) + # set theme & font size
geom_area(color = "white") + # outline areas
scale_fill_brewer(name = "Region", palette = "Set2")
ap # show static area plot

Interactive area plot
ggplotly(ap) # make the area plot interactive with ggplotly