Nations GDP Charts

Author

Robert Gravatt

library(tidyverse)
library(RColorBrewer)

nations <- read_csv("nations.csv") |>
  mutate(gdp = (gdp_percap * population) / 1e12) # 1e12 = trillions
# Filter for four countries
selected_countries <- c("China", "Germany", "Japan", "United States")

nations_filtered <- nations |>
  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
regional_gdp <- nations |>
  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()