introduction to ggalluvial
|
Introduction to ggalluvial
|
|
The ggalluvial package is a ggplot2 extension for producing alluvial plots.
Alluvial plots use variable-width ribbons and stacked bar plots to represent multi-dimensional or repeated-measures data with categorical or ordinal variables. There are two types of alluvial format: Alluvial (Wide) Format & Lodes (Long) Format |
|
More information
|
|
1. AIXS
|
A dimension (variable) along which the data are vertically grouped at a fixed horizontal position.
|
|
2. ALLUVIUM
|
Horizontal (x-) splines called alluvia span the width of the plot.
|
|
3. STRATUM
|
The groups at each axis are depicted as opaque blocks called strata.
|
|
4. LODE
|
The alluvia intersect the strata at lodes.
|
|
5. FLOW
|
The segments of the alluvia between pairs of adjacent axes are flows.
|
|
Discription
|
Example
|
|
load packages
|
library(ggalluvial)
|
|
basic ggplot
|
gg <- ggplot(as.data.frame
(UCBAdmissions), aes(y = Freq, axis1 = Gender, axis2 = Dept, axis3 = Admit)) |
|
add alluvium
|
+ geom_alluvium()
|
|
add stratum
|
+ geom_stratum()
|
|
add text
|
+ geom_text(stat = "stratum", aes(label = paste(after_stat(stratum))))
|
|
add title
|
+ ggtitle("UC Berkeley admissions and rejections")
|
|
the border
of alluvium |
geom_alluvium(color="red")
|
|
the border
of stratum |
geom_stratum(color="blue")
|
|
the fill of alluvium
|
geom_alluvium(aes(fill=Dept))
geom_alluvium(aes(fill=Gender)) geom_stratum(aes(fill=Admit)) |
|
the fill of stratum
|
geom_stratum(aes(fill=Dept))
geom_stratum(aes(fill=Gender)) geom_stratum(aes(fill=Admit)) |
|
the width of alluvium
|
geom_alluvium(color="red",
aes(fill=Dept),width=1/12) |
|
the width of stratum
|
geom_stratum(color="blue",
aes(fill=Dept),width=1/12) |
|
Description
|
Example
|
|
Adding the names of each axis
|
+scale_x_discrete(limits = c("Gender", "Dept","Admit"))
|
|
Changing the fill of stratum
|
+scale_fill_brewer(type = "qual", palette = "Set1")
|
|
Description
|
Example
|
|
Convert data to Lodes format
|
to_lodes_form(as.data.frame(UCBAdmissions),axes = 1:3,id = "Cohort")
|
|
load data
|
data(majors)
majors$curriculum <- as.factor(majors$curriculum) |
|
basic ggplot
|
ggplot(majors,aes(x = semester, stratum = curriculum, alluvium = student,fill = curriculum, label = curriculum))
|
|
add flow
|
+geom_flow(stat = "alluvium", lode.guidance = "frontback",color = "darkgray") +
|
|
add stratum
|
+geom_stratum()
|
|
add title
|
+ggtitle("student curricula across several semesters")
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.