Population pyramid with R

Cedric Vidonne

Lei Chen

Population pyramid with R

A population pyramid consists of two histograms, one for each gender (conventionally, males on the left and females on the right) where the population numbers are shown horizontally (X-axis) and the age vertically (Y-axis).

More about: Population pyramid


Population pyramid

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/distribution/population_pyramid.csv")

# Plot
ggplot(df) +
  geom_col(aes(-male,
    ages,
    fill = "Male"
  ),
  width = 0.7
  ) +
  geom_col(aes(female,
    ages,
    fill = "Female"
  ),
  width = 0.7
  ) +
  geom_text(aes(-male,
    ages,
    label = percent(abs(male))
  ),
  hjust = 1.25,
  size = 11.5 / .pt
  ) +
  geom_text(aes(female,
    ages,
    label = percent(abs(female))
  ),
  hjust = -0.25,
  size = 11.5 / .pt
  ) +
  labs(
    title = "Demographics of forcibly displaced people | 2020",
    caption = "Note: figures do not add up to 100 per cent due to rounding\nSource: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(expand = expansion(c(0.2, 0.2))) +
  scale_fill_manual(values = setNames(
    unhcr_pal(n = 3, "pal_unhcr")[c(2, 1)],
    c("Male", "Female")
  )) +
  theme_unhcr(
    grid = FALSE,
    axis = FALSE,
    axis_title = FALSE,
    axis_text = "y"
  )

A population pyramid showing demographic of forcibly displaced people | 2020


Related chart with R