Streamgraphs

Author

Tejas Shrestha

Published

March 4, 2026

Alluvials

Load the alluvial package

An alluvial diagram shows how categories flow or change across stages (often over time).

Refugees is a prebuilt dataset in the alluvial package

If you want to save the prebuilt dataset to your folder, use the write_csv function

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(alluvial)
library(ggalluvial)
Loading required package: ggplot2
data(Refugees)

Show UNHCR-recognised refugees

Top 10 most affected countries causing refugees from 2003-2013

geom_alluvium()

This function draws the flowing bands (streams) that connect values across the x-axis.

  • Each band = one country’s refugee counts across years.

    The width of the band represents the number of refugees.

ggalluv <- Refugees |>
  ggplot(aes(x = year, y = refugees, alluvium = country)) + 
  theme_bw() +
  geom_alluvium(aes(fill = country), 
                color = "white",
                width = .1, 
                alpha = .89,
                decreasing = FALSE) +
  scale_fill_brewer(palette = "Spectral") + 
  # Spectral has enough colors for all countries listed
  scale_x_continuous(lim = c(2002, 2013)) +
  labs(title = "UNHCR-Recognised Refugees Top 10 Countries\n (2003-2013)",
         # \n breaks the long title
       y = "Number of Refugees", 
       fill = "Country",
       caption = "Source: United Nations High Commissioner for Refugees (UNHCR)")
ggalluv

Plot the Alluvial

ggalluv

A final touch to fix the y-axis scale

Notice the y-values are in scientific notation. We can convert them to standard notation with options scipen function

options(scipen = 999)
ggalluv

TODO

Create an alluvial plot showing how refugee counts change over time from year 2006 to 2012 for countries - Syria, Iraq, Vietnam.

refugee_counts <- Refugees |>
  filter(country =="Syria" | country == "Iraq" | country == "Vietnam") |>
  filter(year != 2003, year != 2004, year != 2005, year != 2013) |>
  arrange(desc(refugees))

head(refugee_counts)
  country year refugees
1    Iraq 2007  2279245
2    Iraq 2008  1873519
3    Iraq 2009  1785212
4    Iraq 2010  1683575
5    Iraq 2006  1450905
6    Iraq 2011  1428308
syria_iraq_vietnam <- refugee_counts |>
  ggplot(aes(x = year, y = refugees, alluvium = country)) + 
  theme_bw() +
  geom_alluvium(aes(fill = country), 
                color = "white",
                width = .1, 
                alpha = .89,
                decreasing = FALSE) +
  scale_fill_brewer(palette = "Spectral") + 
  # Spectral has enough colors for all countries listed
  scale_x_continuous(lim = c(2006, 2012)) +
  labs(title = "Refugee counts for countries Syria, Iraq, and Vietnam (2006-2012)",
         # \n breaks the long title
       y = "Number of Refugees", 
       fill = "Country",
       caption = "Source: United Nations High Commissioner for Refugees (UNHCR)")
syria_iraq_vietnam