Problem Set X: Gapminder

Author

Jane Student

Published

March 5, 2026

library(tidyverse)
library(gapminder)

This report uses the gapminder dataset, which contains panel data on life expectancy, population, and GDP per capita for 142 countries between 1952 and 2007.

Q1: Which countries experienced the largest improvements in life expectancy over time?

To answer this I calculate the change in life expectancy between the first (1952) and last (2007) observation for each country, then look at the top performers.

life_exp_change = gapminder |>
  filter(year %in% c(1952, 2007)) |>
  select(country, continent, year, lifeExp) |>
  pivot_wider(names_from = year, values_from = lifeExp,
              names_prefix = "yr_") |>
  mutate(change = yr_2007 - yr_1952) |>
  arrange(desc(change))

life_exp_change |>
  slice_head(n = 10) |>
  knitr::kable(digits = 1,
               col.names = c("Country", "Continent", "1952", "2007", "Change"),
               caption = "Top 10 countries by life expectancy gains, 1952–2007")
Top 10 countries by life expectancy gains, 1952–2007
Country Continent 1952 2007 Change
Oman Asia 37.6 75.6 38.1
Vietnam Asia 40.4 74.2 33.8
Indonesia Asia 37.5 70.7 33.2
Saudi Arabia Asia 39.9 72.8 32.9
Libya Africa 42.7 74.0 31.2
Korea, Rep. Asia 47.5 78.6 31.2
Nicaragua Americas 42.3 72.9 30.6
West Bank and Gaza Asia 43.2 73.4 30.3
Yemen, Rep. Asia 32.5 62.7 30.1
Gambia Africa 30.0 59.4 29.4

The largest gains are concentrated in Asia and the Middle East. Oman leads with a roughly 40-year increase, reflecting rapid development driven by oil wealth and public health investment. Several East Asian countries (Vietnam, Indonesia, South Korea) also appear, consistent with the region’s dramatic postwar development.

top5 = life_exp_change |> slice_head(n = 5) |> pull(country)

gapminder |>
  filter(country %in% top5) |>
  ggplot(aes(x = year, y = lifeExp, color = country)) +
  geom_line(linewidth = 1) +
  geom_point(size = 1.5) +
  labs(x = NULL, y = "Life expectancy (years)", color = NULL) +
  theme_minimal()
Figure 1: Life expectancy over time for the 5 countries with the largest gains

Figure 1 shows that the gains were not uniform over time—most of the improvement happened between the 1960s and 1990s, with growth leveling off as countries approached the global frontier.

Q2: Is there a relationship between GDP per capita and life expectancy?

The classic Preston curve suggests a positive but concave relationship between income and health. I examine this using the 2007 cross-section.

gapminder |>
  filter(year == 2007) |>
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point(aes(size = pop, color = continent), alpha = 0.7) +
  geom_smooth(method = "loess", se = TRUE, color = "black",
              linewidth = 0.8) +
  scale_x_log10(labels = scales::label_dollar()) +
  scale_size_continuous(guide = "none") +
  labs(x = "GDP per capita (log scale)",
       y = "Life expectancy (years)",
       color = NULL) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
Figure 2: GDP per capita and life expectancy in 2007 (log scale on x-axis)

Figure 2 confirms the expected pattern: life expectancy rises steeply with income at low levels of GDP per capita but flattens out among wealthier countries. A few African countries fall well below the trend, likely reflecting the impact of the HIV/AIDS epidemic. The log transformation of GDP straightens the relationship considerably, which makes sense—the difference between $1,000 and $5,000 matters much more for health than the difference between $30,000 and $34,000.

A simple regression of life expectancy on log GDP per capita quantifies this:

fit = lm(lifeExp ~ log(gdpPercap), data = gapminder |> filter(year == 2007))
summary(fit)

Call:
lm(formula = lifeExp ~ log(gdpPercap), data = filter(gapminder, 
    year == 2007))

Residuals:
    Min      1Q  Median      3Q     Max 
-25.947  -2.661   1.215   4.469  13.115 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      4.9496     3.8577   1.283    0.202    
log(gdpPercap)   7.2028     0.4423  16.283   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.122 on 140 degrees of freedom
Multiple R-squared:  0.6544,    Adjusted R-squared:  0.652 
F-statistic: 265.2 on 1 and 140 DF,  p-value: < 2.2e-16

A one-unit increase in log GDP per capita (roughly a doubling of income) is associated with about a 7.2-year increase in life expectancy. The \(R^2\) of 0.65 indicates that income alone explains a substantial share of cross-national variation in health, though clearly other factors matter.

Q3: How has the distribution of life expectancy changed across continents over time?

Finally, I examine how the distribution of life expectancy has evolved within each continent.

gapminder |>
  filter(year %in% c(1952, 2007)) |>
  mutate(year = factor(year)) |>
  ggplot(aes(x = lifeExp, y = continent, fill = year)) +
  geom_boxplot(alpha = 0.7, position = position_dodge(width = 0.8)) +
  labs(x = "Life expectancy (years)", y = NULL, fill = NULL) +
  theme_minimal()
Figure 3: Distribution of life expectancy by continent, 1952 vs. 2007

Two things stand out in Figure 3. First, every continent shifted rightward between 1952 and 2007—life expectancy improved nearly everywhere. Second, the distributions compressed: there is less within-continent variation in 2007 than in 1952, especially in Asia, where the 1952 distribution was extremely spread out. Africa is the exception—while the median improved, the 2007 distribution remains wide, reflecting divergent trajectories across the continent.

AI disclosure

I used Claude to help debug a pivot_wider call in Q1 and to refine the axis labels in Q2. All interpretive text is my own.