Nations Dataset Charts Assignment

Author

Zixuan Li

library(dplyr)
library(ggplot2)
library(readr)

nations <- read_csv("nations.csv") %>%
  mutate(gdp = gdp_percap * population / 1000000000000)

Chart 1: GDP of Four Countries

big4 <- nations %>%
  filter(iso3c %in% c("CHN", "DEU", "JPN", "USA")) %>%
  arrange(year)

ggplot(big4, aes(x = year, y = gdp, color = country)) +
  geom_point(size = 1.5) +
  geom_line(linewidth = 0.7) +
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP of Four Countries",
       x = "Year", y = "GDP ($ trillion)", color = "Country") +
  theme_minimal()

Chart 2: GDP by Region

regions <- nations %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp, na.rm = TRUE), .groups = "drop") %>%
  arrange(year, region)

ggplot(regions, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by Region",
       x = "Year", y = "GDP ($ trillion)", fill = "Region") +
  theme_minimal()