10  Mapping Global Life Expectancy

10.1 Set up

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rnaturalearth) # For world map data

load("out/expectancy_merged.RData")

10.2 Overview of Life Expectancy

Here let’s start by looking at the summary statistics of worldwide life expectancy in the most recent year available (2022).

# Summary statistics of worldwide life expectancy in 2022
df_merge |>
  filter(year == 2022) |>
  summarise(mean_life_expectancy = mean(life_expectancy),
            median_life_expectancy = median(life_expectancy),
            min_life_expectancy = min(life_expectancy),
            max_life_expectancy = max(life_expectancy))

Top 10 Countries/Regions with Highest Life Expectancy

# Top 10 countries with the highest life expectancy in 2022
df_merge |>
  filter(year == 2022) |>
  arrange(desc(life_expectancy)) |>
  head(10)

10.3 Data Visualization

Bar Plot: Life Expectancy in 2022 by 10 Major Countries

# List of 10 major countries (with highest GDP)
country_list <- c("United States", "China", "Japan", "Germany", "India", 
                  "United Kingdom", "France", "Brazil", "Italy", "Canada")
df_merge |>
  filter(year == 2022) |>
  filter(country %in% country_list) |>
  ggplot(aes(x = reorder(country, life_expectancy), y = life_expectancy)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(title = "Life Expectancy in 2022 by 10 Major Countries",
       x = "Country",
       y = "Life Expectancy at Birth",
       fill = "Country",
       caption = "Source: World Bank | Author: Bin Chen") +
  geom_text(aes(label = round(life_expectancy, 1)), hjust = -0.1, size = 3) +
  theme_minimal()

Line Plot: Life expectancy trend in China

# Life expectancy trend in China
df_merge |>
  filter(country == "China") |>
  ggplot(aes(x = year, y = life_expectancy)) +
  geom_line(color = "steelblue") +
  labs(title = "Life Expectancy Trend in China",
       x = "Year",
       y = "Life Expectancy at Birth",
       caption = "Source: World Bank | Author: Bin Chen") +
  theme_minimal()

Multiple Lines

# Life expectancy trend in the 10 major countries
df_merge |>
  filter(country %in% country_list) |>
  ggplot(aes(x = year, y = life_expectancy, color = country)) +
  geom_line() +
  labs(title = "Life Expectancy Trend in 10 Major Countries",
       x = " ",
       y = " ",
       caption = "Source: World Bank | Author: Bin Chen") +
  theme_minimal() +
  facet_wrap(~country, scales = "free_y") + 
  theme(legend.position = "none")

Map Plot: Life Expectancy by Country in 2022

# Map of life expectancy by country in 2022
df_merge |>
  filter(year == 2022) |>
1  ggplot(aes()) +
2  geom_sf(aes(geometry = geometry, fill = life_expectancy)) +
  scale_fill_gradient(low = "white", 
                      high = "#3182bd",
                      na.value = "grey90", 
                      name = "Life Expectancy", 
                      guide = guide_colorbar(
                        barwidth = 10,
                        barheight = 0.5,
                        title.position = "top",
                        title.hjust = 0.5
3                      )) +
  labs(title = "Life Expectancy at Birth by Country in 2022",
       fill = "Life Expectancy at Birth",
4       caption = "Source: World Bank | By: Bin Chen") +
5  theme_void() +
  theme(legend.position = "bottom",
        legend.title = element_text(size = 10),
6        plot.title = element_text(hjust = 0.5, size = 11))
1
Set up the plot.
2
Create a spatial feature plot using the world map geometry and fill color based on life expectancy.
3
Set the color gradient for life expectancy.
4
Add titles and captions.
5
Remove the background.
6
Adjust the legend position and title size.