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)
Warning: package 'ggalluvial' was built under R version 4.5.2
Loading required package: ggplot2
Warning: package 'ggplot2' was built under R version 4.5.2
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 listedscale_x_continuous(lim =c(2002, 2013)) +labs(title ="UNHCR-Recognised Refugees Top 10 Countries\n (2003-2013)",# \n breaks the long titley ="Number of Refugees", fill ="Country",caption ="Source: United Nations High Commissioner for Refugees (UNHCR)")
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.
ggalluv1 <- refugees2 |>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 listedscale_x_continuous(lim =c(2006, 2012)) +labs(title ="UNHCR-Recognised Refugees Top 10 Countries\n (2003-2013)",# \n breaks the long titley ="Number of Refugees", fill ="Country",caption ="Source: United Nations High Commissioner for Refugees (UNHCR)")