library(tidyverse)
library(RColorBrewer)
<- read_csv("nations.csv") |>
nations mutate(gdp = (gdp_percap * population) / 1e12) # 1e12 = trillions
Nations GDP Charts
# Filter for four countries
<- c("China", "Germany", "Japan", "United States")
selected_countries
<- nations |>
nations_filtered filter(country %in% selected_countries)
# Plot with points and lines
ggplot(nations_filtered, aes(x = year, y = gdp, color = country)) +
geom_line(linewidth = 0.5) +
geom_point(size = 1) +
scale_color_brewer(palette = "Set1") +
labs(title = "GDP Over Time (Trillions USD)",
x = "Year",
y = "GDP (Trillions)",
color = "Country") +
theme_minimal()
# Summarize GDP by region and year
<- nations |>
regional_gdp group_by(region, year) |>
summarize(GDP = sum(gdp, na.rm = TRUE), .groups = "drop")
# ?summarize() lists .groups = "drop" in Arguments. It returns an ungrouped df.
# Area chart
ggplot(regional_gdp, aes(x = year, y = GDP, fill = region)) +
geom_area(color = "white", linewidth = 0.2) +
scale_fill_brewer(palette = "Set2") +
labs(title = "Regional GDP Over Time (Trillions USD)",
x = "Year",
y = "GDP (Trillions)",
fill = "Region") +
theme_minimal()