ggalluvial:如何让区块间流线条顺序与数据框一致?
桑基图调整需求与解决方法
需求
使用ggalluvial绘制桑基图时,需要解决两个问题:
- 让黄色(Male)流线条始终位于紫色(Female)线条上方(覆盖x轴所有区块)
- 修复区块内文本位置错位的问题
已尝试排序数据框、指定因子层级、调整lode_order参数,但未达到预期效果。
可复现代码
原始数据创建代码
w <- c("Rain Forest", "Desert", "Arctic", "City", "Low Earth", "Boonies", "Space") x <- 1:40 y <- c("Bird", "Tiger", "Cat", "Parrot", "T-Rex", "Dreadwing") set.seed(0) GenComp <- data.frame( Location = sample(w, size = 28, replace = TRUE), Frequency = sample(x = x, size = 28, replace = TRUE), Animal = sample(x = y, size = 28, replace = TRUE), Gender = rep(c("Male", "Female"), 14)) GenComp <- GenComp[order(GenComp$Gender, GenComp$Animal, GenComp$Location, decreasing = FALSE),] row.names(GenComp) <- 1:nrow(GenComp) GenComp$Location <- factor(GenComp$Location, levels = sort(unique(GenComp$Location))) GenComp$Gender <- factor(GenComp$Gender, levels = c("Female", "Male")) GenComp$Animal <- factor(GenComp$Animal, levels = sort(unique(GenComp$Animal))) GenComp <- GenComp[, c(4, 3, 1, 2)]
原始绘图代码
library(ggalluvial) ggplot(GenComp,aes(y = Frequency, axis1 = Gender, axis2 = Animal, axis3 = Location)) + geom_alluvium(aes(fill = Gender), width = 1/8, knot.pos = 0, reverse = FALSE) + geom_stratum(alpha = 0.25, width = 1/8, reverse = FALSE) + geom_text(stat = "stratum", aes(label = after_stat(stratum)), reverse = TRUE, size = 3) + ggtitle("Distribution of Fauna") + scale_fill_manual(values = c(Female = "#691869", Male = "#EBBC55")) + guides(fill = "none")
解决方法
关键调整思路
- 统一流线条顺序:通过调整因子层级和排序规则,让Male相关的流线优先排列在上;同时在绘图层强制按Gender排序流向。
- 修正文本位置:统一所有图层的
reverse参数,让文本与区块的顺序完全匹配。
修改后的完整代码
# 创建数据框(调整排序与因子层级) w <- c("Rain Forest", "Desert", "Arctic", "City", "Low Earth", "Boonies", "Space") x <- 1:40 y <- c("Bird", "Tiger", "Cat", "Parrot", "T-Rex", "Dreadwing") set.seed(0) GenComp <- data.frame( Location = sample(w, size = 28, replace = TRUE), Frequency = sample(x = x, size = 28, replace = TRUE), Animal = sample(x = y, size = 28, replace = TRUE), Gender = rep(c("Male", "Female"), 14)) # 按Gender降序排序,确保Male相关行在前 GenComp <- GenComp[order(GenComp$Gender, GenComp$Animal, GenComp$Location, decreasing = TRUE),] row.names(GenComp) <- 1:nrow(GenComp) # 调整Gender因子层级,Male在前 GenComp$Location <- factor(GenComp$Location, levels = sort(unique(GenComp$Location))) GenComp$Gender <- factor(GenComp$Gender, levels = c("Male", "Female")) GenComp$Animal <- factor(GenComp$Animal, levels = sort(unique(GenComp$Animal))) GenComp <- GenComp[, c(4, 3, 1, 2)] # 绘图代码(修正参数) library(ggalluvial) ggplot(GenComp,aes(y = Frequency, axis1 = Gender, axis2 = Animal, axis3 = Location)) + # 添加order = Gender强制按性别排序流线 geom_alluvium(aes(fill = Gender, order = Gender), width = 1/8, knot.pos = 0, reverse = TRUE) + geom_stratum(alpha = 0.25, width = 1/8, reverse = TRUE) + # 统一reverse参数,添加order绑定文本与区块顺序 geom_text(stat = "stratum", aes(label = after_stat(stratum), order = after_stat(stratum)), reverse = TRUE, size = 3) + ggtitle("Distribution of Fauna") + scale_fill_manual(values = c(Male = "#EBBC55", Female = "#691869")) + guides(fill = "none")
调整点说明
- 数据排序时设置
decreasing = TRUE,配合Gender因子层级,让Male的记录排在数据集前面 geom_alluvium中添加order = Gender,强制按性别因子层级排列流线,保证Male流线始终在上- 统一所有图层的
reverse参数为TRUE,避免流线、区块、文本的顺序错位 - 文本图层添加
order = after_stat(stratum),让文本位置与区块顺序完全对应,解决错位问题
内容的提问来源于stack exchange,提问作者Purrsia
相关产品推荐
相关产品推荐

