Nations Charts

Author

Emmanuel Gkatongoni

Nations Chart Assignment

Loading the Data

After loading in our libraries I then load the nations dataset into R and use mutate() to create a new gdp column by multiplying gdp_percap by populationto get each country’s total GDP, then dividing by a trillion for management of numbers.

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
# Load the data
nations <- read.csv("nations.csv")

# Create gdp variable (GDP in trillions of dollars)
nations <- nations %>%
  mutate(gdp = gdp_percap * population / 1e12)

China’s Rise to Become the Largest Economy

I filter the data for four countries and plot their GDP over time using points and lines. Each country is shown in a different color using the Set1 palette.

# Filter for the four countries
chart1_data <- nations %>%
  filter(country %in% c("China", "Germany", "Japan", "United States"))

# Draw the chart
ggplot(chart1_data, aes(x = year, y = gdp, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "China's Rise to Become the Largest Economy",
    x = "year",
    y = "GDP ($ trillion)",
    color = NULL
  ) +
  theme_minimal()

GDP by World Bank Region

I group the data by region and year, then sum up the GDP for each group. The chart uses filled areas to show how each region has contributed to global GDP over time, with each region shown in a different color using the Set2 palette.

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

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