TVP

# 冲击图和桑基图，傻傻分不清楚！

1.1K0

http://corybrunson.github.io/ggalluvial/articles/ggalluvial.html

1. axis, 即x轴代表的元素，在图中表现为每个竖直方向堆叠的柱状图，Gender, Dept

2. strata, 每个axis的分类标准，比如Gender的Strata 对应Male, Female

3. Freq, 即y轴代表的元素，对于每一个竖直方向的柱子，其各个子类Freq的总和是相同的

``````> library(ggalluvial)
2 Rejected   Male    A  313
4 Rejected Female    A   19
6 Rejected   Male    B  207``````

``````> ggplot(data_wide, aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
+   geom_alluvium() +
+   geom_stratum() +
+   geom_label(stat = "stratum", aes(label = after_stat(stratum)))``````

1. 把柱子的填充色设置为黑色

2. 把柱子的宽度变窄

4. 添加x轴标签

``````> ggplot(data_wide, aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
+   geom_alluvium(aes(fill = Admit), width = 1/4) +
+   geom_stratum(fill = "black", colour = "white", width = 1/4) +
+   geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
+   scale_x_continuous(breaks = 1:2, labels = c("Gender", "Dept"))``````

``````> data_long <- to_lodes_form(data_wide, axies = 2:3,  id = "num")
1 Admitted  512   1 Gender    Male
2 Rejected  313   2 Gender    Male
3 Admitted   89   3 Gender  Female
4 Rejected   19   4 Gender  Female
5 Admitted  353   5 Gender    Male
6 Rejected  207   6 Gender    Male
> ggplot(data_long,
+   aes(x = x, y = Freq, fill =  Admit, stratum = stratum, alluvium = num)) +
+   geom_flow() +
+   geom_stratum(fill = "black", colour = "white") +
+   geom_label(stat = "stratum", aes(label = after_stat(stratum)), fill = "white")``````

``````> ggplot(vaccinations,
+        aes(x = survey, stratum = response, alluvium = subject,
+            y = freq,
+            fill = response, label = response)) +
+   scale_x_discrete(expand = c(.1, .1)) +
+   geom_flow() +
+   geom_stratum(alpha = .5) +
+   geom_text(stat = "stratum", size = 3) +
+   theme(legend.position = "none") +
+   ggtitle("vaccination survey responses at three points in time")``````

·end·

0 条评论