Alluvial plot冲积图
1.ggalluvial包绘制Alluvial plot,使用的数据集是R自带的vaccinations,简单绘图如下:
library(ggalluvial)
ggplot(data = vaccinations,
aes(axis1 = survey, axis2 = response, y = freq)) +
geom_alluvium(aes(fill = response)) +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
theme_void()
2.如果数据集包含更多类别变量,可以将它们传递给aes(aex1,aex2,…等等)
library(ggalluvial)
ggplot(data = vaccinations,
aes(axis1 = survey, # First variable on the X-axis
axis2 = response, # Second variable on the X-axis
axis3 = survey, # Third variable on the X-axis
y = freq)) +
geom_alluvium(aes(fill = response)) +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
theme_void()
3.使用geom_alluvium函数调整曲线形状,有Linear、Cubic、Quintic、Sine、Arctangent、Sigmoid几种类型可供调整;
library(ggalluvial)
ggplot(data = vaccinations,
aes(axis1 = survey, axis2 = response, y = freq)) +
geom_alluvium(aes(fill = response),
curve_type = "linear") + ####直接在此替换调整即可
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
theme_void()
4.调整颜色;
library(ggalluvial)
#颜色
colors=c( "#3C5488B2","#00A087B2",
"#F39B7FB2","#91D1C2B2",
"#8491B4B2", "#DC0000B2",
"#7E6148B2","yellow",
"darkolivegreen1", "lightskyblue",
"darkgreen", "deeppink", "khaki2",
"firebrick", "brown1", "darkorange1",
"cyan1", "royalblue4", "darksalmon",
"darkgoldenrod1", "darkseagreen", "darkorchid")
ggplot(data = vaccinations,
aes(axis1 = survey, axis2 = response, y = freq)) +
geom_alluvium(aes(fill = response),
curve_type = "linear") +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
scale_fill_manual(values = colors) +
theme_void()
5.调整图例等相关参数。
######或者去除图例说明
library(ggalluvial)
#颜色
colors=c( "#3C5488B2","#00A087B2",
"#F39B7FB2","#91D1C2B2",
"#8491B4B2", "#DC0000B2",
"#7E6148B2","yellow",
"darkolivegreen1", "lightskyblue",
"darkgreen", "deeppink", "khaki2",
"firebrick", "brown1", "darkorange1",
"cyan1", "royalblue4", "darksalmon",
"darkgoldenrod1", "darkseagreen", "darkorchid")
ggplot(data = vaccinations,
aes(axis1 = survey, axis2 = response, y = freq)) +
geom_alluvium(aes(fill = response),
curve_type = "linear") +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
scale_fill_manual(values = colors) +
theme_void() +
theme(legend.position = "none")
##改变主题
library(ggalluvial)
colors=c( "#3C5488B2","#00A087B2",
"#F39B7FB2","#91D1C2B2",
"#8491B4B2", "#DC0000B2",
"#7E6148B2","yellow",
"darkolivegreen1", "lightskyblue",
"darkgreen", "deeppink", "khaki2",
"firebrick", "brown1", "darkorange1",
"cyan1", "royalblue4", "darksalmon",
"darkgoldenrod1", "darkseagreen", "darkorchid")
ggplot(data = vaccinations,
aes(axis1 = survey, axis2 = response, y = freq)) +
geom_alluvium(aes(fill = response),
curve_type = "linear") +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Survey", "Response"),
expand = c(0.15, 0.05)) +
scale_fill_manual(values = colors) +
theme_void() +
guides(fill = guide_legend(title = "Tittle"))
关注“作图帮”公众号,免费分享绘图代码与示例数据~
转载:https://blog.csdn.net/weifanbio/article/details/117248480
查看评论