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 = .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)")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)
ggalluv
TODO
Create an alluvial plot showing how refugee counts change over time from year 2006 to 2012 for countries - Syria, Iraq, Vietnam.
Refu1 <- Refugees |>
filter(year %in% c(2006, 2007, 2008, 2009, 2010, 2011, 2012)) |>
filter(country %in% c("Syria", "Iraq", "Vietnam"))
refugraph <- Refu1 |>
ggplot(aes(x = year, y = refugees, alluvium = country)) +
theme_bw() +
geom_alluvium(aes(fill = country),
color = "white",
width = 0.1,
alpha = 0.89,
decreasing = FALSE) +
scale_fill_brewer(palette = "Spectral") +
scale_x_continuous(lim = c(2006, 2012)) +
labs(title = "Refugee counts over time from 2006 to 2012 in Syria, Iraq, and Vietnam",
y = "# of Refugees",
fill = "Country",
caption = "Source: United Nations High Commissioner for Refugees (UNHCR)")
options(scipen = 999)
refugraph