I am half Polish so out of curiosity I wanted to compare the GDP of Poland vs the United States from 2019-2023.

Question

How did GDP per capita in Poland compare to the United States between 2019-2023.

Load Data

# Load CSV (I skipped 4 rows because they are just metadata)
data <- read.csv("API_NY.GDP.PCAP.CD_DS2_en_csv_v2_870044.csv", skip = 4)

# Keep only USA and Poland
gdp_data <- filter(data, Country.Name == "United States" | Country.Name == "Poland")

# Selecting only the last 5 years
gdp_data <- select(gdp_data, Country.Name, X2019, X2020, X2021, X2022, X2023)

#renaming the years so it doees not include x
gdp_data <- rename(gdp_data,
                   "2019" = X2019,
                   "2020" = X2020,
                   "2021" = X2021,
                   "2022" = X2022,
                   "2023" = X2023)
gdp_data
##    Country.Name     2019     2020     2021     2022     2023
## 1        Poland 15874.52 16150.93 18635.51 18891.21 22145.26
## 2 United States 65227.96 64401.51 71307.40 77860.91 82304.62

Reshaping the data so the years are all in one column.

# I used gather to make years into one column
gdp_long <- gdp_data %>%
  gather(`2019`:`2023`, key = "Year", value = "GDP_per_capita")

gdp_long$Year <- as.numeric(gdp_long$Year)
gdp_long
##     Country.Name Year GDP_per_capita
## 1         Poland 2019       15874.52
## 2  United States 2019       65227.96
## 3         Poland 2020       16150.93
## 4  United States 2020       64401.51
## 5         Poland 2021       18635.51
## 6  United States 2021       71307.40
## 7         Poland 2022       18891.21
## 8  United States 2022       77860.91
## 9         Poland 2023       22145.26
## 10 United States 2023       82304.62

Summarize with dplyr

# 1. Average GDP per capita for each country
avg_table <- gdp_long %>%
  group_by(Country.Name) %>%
  summarize(Average_GDP = mean(GDP_per_capita, na.rm = TRUE))

avg_table
## # A tibble: 2 × 2
##   Country.Name  Average_GDP
##   <chr>               <dbl>
## 1 Poland             18339.
## 2 United States      72220.

Plotting the data

ggplot(gdp_long, aes(x = Year, y = GDP_per_capita, color = `Country.Name`)) +
  geom_line(size = 1.2) +
  geom_point(size = 2) +
  labs(title = "GDP per Capita (2019–2023): USA vs Poland",
       x = "Year", y = "GDP per capita (US$)",
       color = "Country") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Insights

  1. The United States has a much higher GDP per capita than Poland in every year.
  2. Poland shows steady growth, while the US also increases but remains far ahead.