Streamgraph with R

Cedric Vidonne

Lei Chen

Streamgraph with R

A streamgraph is a variation of a stacked area chart. Instead of displaying values on top of a fixed, straight baseline at the bottom of the stack, the values of the streamgraph are displaced around a central baseline.

More about: Streamgraph


Streamgraph using ggstream package

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

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

# Plot
ggplot(
  df,
  aes(
    x = year,
    y = population_number,
    fill = factor(population_type,
      levels = c("REF", "ASY", "IDP", "STA", "OOC", "VDA")
    )
  )
) +
  geom_stream() +
  scale_fill_unhcr_d(
    palette = "pal_unhcr_poc",
    nmax = 9,
    order = c(1, 3:5, 8:9),
    labels = c("Refugees", "Asylum-seekers", "IDPs", "Stateless persons", "Others of concern", "Venezuelans displaced abroad")
  ) +
  labs(
    title = "People of concern to UNHCR | 1991-2021",
    y = "Number of people",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(breaks = pretty_breaks(n = 8)) +
  scale_y_continuous(
    labels = label_number_si(),
    breaks = pretty_breaks(n = 6)
  ) +
  theme_unhcr(
    grid = "XY",
    axis_title = "y",
    axis = FALSE
  )

A streamgraph showing evolution of people of concern to UNHCR | 1991-2020


Related chart with R