library(tidyverse)
library(RColorBrewer)
library(plotly)HW- Nations dataset
Set Libraries
Import dataset
nations<- read_csv("nations.csv")GDP of each country in trillions of dollars
nations_gdp <- nations |>
mutate(gdp_trillions = gdp_percap*population/1000000000000)Plot 1:
Filter the four countries
nations4<- nations_gdp|>
filter(country %in% c("China",
"United States",
"Germany",
"Japan")
)p1<- ggplot(nations4, aes(year, gdp_trillions, color = country)) +
geom_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()
p1Interactive Plot 1
ggplotly(p1)Set the dataset
region_data <- nations_gdp |>
group_by(region, year) |>
summarise(sum_GDP = sum(gdp_trillions, na.rm = TRUE))Plot 2
p2 <- ggplot(region_data, aes(year, sum_GDP, fill = region)) +
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))
p2Interactive Plot 2
ggplotly(p2)