如何在ggplot的Alluvial图中调整Stratum分层框的边框线宽?
调整Alluvial图分层框边框线宽的方案
问题说明
使用ggplot结合ggalluvial绘制Alluvial图时,分层框(stratum)辨识度不足,希望在不修改颜色设置的前提下,仅增大左右两侧分层框的边框线宽,不调整框之间连接线的宽度。
原绘图代码
library(dplyr) library(ggplot2) library("ggalluvial") library("viridis") ggplot(df, aes(y = Total_Publications, axis1 = Method_Group, axis2 = Topic)) + geom_alluvium(aes(fill = Topic, color = after_scale(fill)), width = 1/5, alpha = 1) + scale_fill_viridis(discrete=TRUE) + geom_stratum(width = 1/5, alpha = 0.2, reverse = TRUE, color = "black") + geom_text(stat = "stratum", aes(label = after_stat(stratum)), size= 5, color = "black") + scale_x_discrete(limits = c("Ecological Topic", "Ecological Theme"), expand = c(.05, .05)) + theme_minimal() + theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())
修改后的代码(实现需求)
library(dplyr) library(ggplot2) library("ggalluvial") library("viridis") ggplot(df, aes(y = Total_Publications, axis1 = Method_Group, axis2 = Topic)) + geom_alluvium(aes(fill = Topic, color = after_scale(fill)), width = 1/5, alpha = 1) + scale_fill_viridis(discrete=TRUE) + # 针对左右两侧轴的分层框设置线宽映射 geom_stratum(width = 1/5, alpha = 0.2, reverse = TRUE, aes(size = after_stat(axis %in% c(1,2))), color = "black") + # 自定义线宽值:两侧分层框线宽设为2,其他保持默认1 scale_size_manual(values = c(`TRUE` = 2, `FALSE` = 1)) + geom_text(stat = "stratum", aes(label = after_stat(stratum)), size= 5, color = "black") + scale_x_discrete(limits = c("Ecological Topic", "Ecological Theme"), expand = c(.05, .05)) + theme_minimal() + theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())
关键修改点
- 在
geom_stratum中,通过after_stat(axis %in% c(1,2))识别左右两侧的轴(axis=1对应左侧,axis=2对应右侧),将线宽与该判断结果绑定 - 使用
scale_size_manual指定线宽:满足条件的分层框(左右两侧)线宽设为2,其余保持默认1(如果有中间轴的话) - 该方法仅调整分层框的边框线宽,不会影响
geom_alluvium绘制的连接线宽度
内容的提问来源于stack exchange,提问作者Kristen Cyr
相关产品推荐
相关产品推荐

