You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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())

关键修改点

  1. 在geom_stratum中,通过after_stat(axis %in% c(1,2))识别左右两侧的轴(axis=1对应左侧,axis=2对应右侧),将线宽与该判断结果绑定
  2. 使用scale_size_manual指定线宽:满足条件的分层框(左右两侧)线宽设为2,其余保持默认1(如果有中间轴的话)
  3. 该方法仅调整分层框的边框线宽,不会影响geom_alluvium绘制的连接线宽度

内容的提问来源于stack exchange,提问作者Kristen Cyr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:24:56