library(tidyverse)
library(RColorBrewer)
library(plotly)
HW- Nations dataset
Set Libraries
Import dataset
<- read_csv("nations.csv") nations
GDP of each country in trillions of dollars
<- nations |>
nations_gdp mutate(gdp_trillions = gdp_percap*population/1000000000000)
Plot 1:
Filter the four countries
<- nations_gdp|>
nations4filter(country %in% c("China",
"United States",
"Germany",
"Japan")
)
<- ggplot(nations4, aes(year, gdp_trillions, color = country)) +
p1geom_point() +
geom_line() +
scale_color_brewer(palette = "Set1") +
labs(title = "China's Rise to Become the Largest Economy")+
scale_x_continuous(name = "Year") +
scale_y_continuous(name = "GDP (Trillions of $)")+
theme_minimal()
p1
Interactive Plot 1
ggplotly(p1)
Set the dataset
<- nations_gdp |>
region_data group_by(region, year) |>
summarise(sum_GDP = sum(gdp_trillions, na.rm = TRUE))
Plot 2
<- ggplot(region_data, aes(year, sum_GDP, fill = region)) +
p2 geom_area(color = "white", #very thin white line around each area
linewidth = 0.1) +
scale_fill_brewer(palette = "Set2") +
theme_minimal() +
labs(title = "GDP by World Bank Region") +
scale_x_continuous(name = "Year") +
scale_y_continuous(name = "GDP ($ Trillion)")
#If you needed to angle the lables on x-axis, use
#theme(axis.text.x = element_text(angle = 45, hjust = 1))
p2
Interactive Plot 2
ggplotly(p2)