Show Menu
Cheatography

ggalluvial Cheat Sheet by

introduction to ggalluvial

GGAL­LUVIAL ---- Cheats­heet

Introd­uction to ggalluvial
The ggalluvial package is a ggplot2 extension for producing alluvial plots.
Alluvial plots use variab­le-­width ribbons and stacked bar plots to represent multi-­dim­ens­ional or repeat­ed-­mea­sures data with catego­rical or ordinal variables.
There are two types of alluvial format:
Alluvial (Wide) Format & Lodes (Long) Format
More inform­ation

Five Essential Components

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.

Basic Alluvial Wide Format

Discription
Example
load packages
librar­y(g­gal­luvial)
basic ggplot
gg <- ggplot(as.data.frame
(UCBAdmissions),
aes(y = Freq, axis1 = Gender,
axis2 = Dept, axis3 = Admit))
add alluvium
+ geom_a­llu­vium()
add stratum
+ geom_s­­tr­a­tum()
add text
+ geom_t­­ex­t­(stat = "­­st­r­a­tu­­m", aes(label = paste(­­af­t­e­r_­­sta­­t(­s­t­ra­­tum))))
add title
+ ggtitl­e("UC Berkeley admissions and reject­ion­s")
The dataset "­UCB­Adm­iss­ion­s" is an aggregate data on applicants to graduate school at Berkeley for the six largest depart­ments in 1973 classified by admission and sex.
It is a 3-dime­nsional array resulting from cross-­tab­ulating 4526 observ­ations on 3 variables.
No   Name   Levels
1   Admit  ­Adm­itted, Rejected
2   Gender   Male, Female
3   Dept   A, B, C, D, E, F

Graph of Wide Format

 

Change Color

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))
Setting the different fills of alluvium and stratum can help analysts easily analyze the data from different aspects.

Graph after changing Color & Fill by Dept

If using "­fil­l=D­ept­", it means we are using colors grouped by each depart­ment.
It can help analyst to see the formation of each depart­ment: how many males and females in each depart­ment.
Also it shows how many people in each department are admitted and rejected.

Graph after changing Color & Fill by Gender

If using "­fil­l=G­end­er", it means we are using colors grouped by different gender.
It can help analyst to see how many males and females apply for each department and finally admitted or rejected.

Graph after changing Color & Fill by Admit

If using "­fil­l=A­dmi­t", it means we are using colors grouped by admitted or rejected.
It can help analyst to see the formation admitted students: how many admitted students are from each department and of different gender.
 

Change Width

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)

Graph after changing Width

Flip Coordi­nates

Adding coord_­flip()
Flip cartesian coordi­nates so that horizontal becomes vertical, and vertical, horizo­ntal. This is primarily useful for converting geoms and statistics which display y condit­ional on x, to x condit­ional on y.

Adding lode

Adding geom_l­ode()

geom_a­lluvium vs geom_flow

The graph is using geom_flow.
We can see the difference between geom_a­lluvium and geom_flow.
After we use "­flo­w", all males apply for department A came together, which is also the same as other depart­ments. It makes the graph much clearer than before since there is less cross alluviums between each axises.

More coding help

Descri­ption
Example
Adding the names of each axis
+scale­_x_­dis­cre­te(­limits = c("G­end­er", "­Dep­t","A­dmi­t"))
Changing the fill of stratum
+scale­_fi­ll_­bre­wer­(type = "­qua­l", palette = "­Set­1")

Basic Lodes (Long) Format

Descri­ption
Example
Convert data to Lodes format
to_lod­es_­for­m(a­s.d­ata.fr­ame­(UC­BAd­mis­sio­ns)­,axes = 1:3,id = "­Coh­ort­")
load data
data(m­ajors)
majors­$cu­rri­culum <- as.fac­tor­(ma­jor­s$c­urr­iculum)
basic ggplot
ggplot­(ma­jor­s,aes(x = semester, stratum = curric­ulum, alluvium = studen­t,fill = curric­ulum, label = curric­ulum))
add flow
+geom_­flo­w(stat = "­all­uvi­um", lode.g­uidance = "­fro­ntb­ack­"­,color = "­dar­kgr­ay") +
add stratum
+geom_­str­atum()
add title
+ggtit­le(­"­student curricula across several semest­ers­")
The long format requires an additional indexing column that links the rows corres­ponding to a common cohort.
The data follows the major curricula of 10 students across 8 academic semesters. Missing values indicate undeclared majors.
A data frame with 80 rows and 3 variables:
1. student: student identifier
2. semester: character tag for odd-nu­mbered semesters
3. curric­ulum: declared major program

Graph of Lodes Format

This graph clearly shows a set of students’ academic curricula over the course of several semesters.
The lode format gives us the option to aggregate the flows between adjacent axes, which may be approp­riate when the transi­tions between adjacent axes are of primary import­ance.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.