generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 139
/
ggalluvial_cheatsheet2.Rmd
89 lines (57 loc) · 2.5 KB
/
ggalluvial_cheatsheet2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# ggalluvial Cheatsheet
Meggie Wen
##Intro 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.
## Description:
Alluvial plots consist of multiple horizontally-distributed columns (axes) representing factor variables, vertical divisions (strata) of these axes representing these variables’ values; and splines (alluvial flows) connecting vertical subdivisions (lodes) within strata of adjacent axes representing subsets or amounts of observations that take the corresponding values of the corresponding variables.
This dataframe is originally created for illustration:
```{r}
library(tidyverse)
library(ggalluvial)
df <- data.frame(x1 = c("A","B","C","A","A","A","B"),
x2 = c("X","X","X","Y","Y","Y","Y"),
x3 = c("1","2","3","3","3","3","3"),
Freq = c(1,2,3,4,5,6,7))
```
**Basic Alluvial Plot:**
ggplot(df, aes(axis1 = x1, axis2 = x2, y = Freq)) +
geom_alluvium() +
geom_stratum() +
geom_text(stat = "stratum", aes(label = paste(after_stat(stratum))))
**Example 1:**
```{r}
ggplot(df, aes(axis1 = x1, axis2 = x2, y = Freq)) +
geom_alluvium() +
geom_stratum() +
geom_text(stat = "stratum", aes(label = paste(after_stat(stratum))))
```
**Add color:**
• Set all lines into one color:
geom_alluvium(color = “blue”, fill = “blue”)
• Set color by categories:
geom_alluvium(aes(color = x1, fill = x1))
• Set stratum color:
geom_stratum(aes(fill = x1))
**Add label:**
scale_x_discrete(limits = c("Step 1", "Step 2”))
**Change curve type:**
geom_alluvium(color = “blue”, fill = “blue”), curve_type = “linear”)
**Example 2:**
```{r}
ggplot(df, aes(axis1 = x1, axis2 = x2, axis3 = x3, y = Freq)) +
geom_alluvium(color = "blue", fill = "blue",curve_type = "linear") +
geom_stratum() +
geom_text(stat = "stratum", aes(label = paste(after_stat(stratum)))) +
scale_x_discrete(limits = c("Step 1", "Step 2"))
```
**Multi-variable Alluvial Plot:**
ggplot(data = df, aes(axis1 = x1, axis2 = x2, axis3 = x3, ..., y = frequency)
**Example 3:**
```{r}
ggplot(df, aes(axis1 = x1, axis2 = x2, axis3 = x3, y = Freq)) +
geom_alluvium(aes(color = x1, fill = x1)) +
geom_stratum(aes(color = x1)) +
geom_text(stat = "stratum", aes(label = paste(after_stat(stratum)))) +
scale_x_discrete(limits = c("Step 1", "Step 2"))
```