library(ggplot2)
library(dplyr)
library(alluvial)
library(ggalluvial)
data(Refugees)Streamgraphs
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
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 = .7,
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)")Plot the Alluvial
ggalluvA 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)
ggalluvTODO
Create an alluvial plot showing how refugee counts change over time from year 2006 to 2012 for countries - Syria, Iraq, Vietnam.
refugees1 <- Refugees |>
filter(year %in% c(2006:2012),
country %in% c("Syria", "Iraq", "Vietnam"))ggalluv2 <- refugees1 |>
ggplot(aes(x = year, y = refugees, alluvium = country)) +
theme_bw() +
geom_alluvium(aes(fill = country),
color = "white",
width = .1,
alpha = .7,
decreasing = FALSE) +
scale_fill_brewer(palette = "Spectral") +
# Spectral has enough colors for all countries listed
scale_x_continuous(lim = c(2006, 2012)) +
labs(title = "UNHCR-Recognised Refugees in Syria, Iraq, & Vietnam \n (2006-2012)",
# \n breaks the long title
y = "Number of Refugees",
fill = "Country",
caption = "Source: United Nations High Commissioner for Refugees (UNHCR)")
ggalluv2