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

ggalluvial桑基图第二轴无法按因子水平排序问题求助

ggalluvial桑基图第二轴不遵循因子层级排序的解决方案

已将桑基图的两个轴转换为因子变量并指定了自定义显示层级,第一轴能正确按照因子层级排序,但第二轴始终不遵循自身的因子设置,反而与第一轴的重叠类别呈镜像排列,修改因子层级也无响应。

数据代码

top_transitions <- data.frame(
  last_pre_tx_drug = c("DPP4 inhibitors", "DPP4 inhibitors",
                       "Insulin", "Insulin", "Insulin", 
                       "None", "None", "None", "None", "None",
                       "Sulfonylureas", "Sulfonylureas", 
                       "Sulfonylureas", "Sulfonylureas"),

  post_transplant_drug = c("Insulin", "Insulin + DPP4 inhibitors", 
                           "Insulin", "None", "Insulin + GLP-1 RAs", 
                           "DPP4 inhibitors", "Insulin", "None", 
                           "Biguanides", "Sulfonylureas", 
                           "Insulin + Sulfonylureas", "Insulin", 
                           "None", "Sulfonylureas"),

  N = c(1809, 1005, 35053, 4506, 808, 2200, 12087, 36052, 
        1304, 3303, 1301, 5017, 1302, 1207)
)

top_transitions$last_pre_tx_drug <- factor(top_transitions$last_pre_tx_drug, 
      levels = c("DPP4 inhibitors", "Insulin", "None", "Sulfonylureas"))

top_transitions$post_transplant_drug <- factor(top_transitions$post_transplant_drug, 
       levels = c("DPP4 inhibitors", "Insulin + Sulfonylureas",
                  "Insulin", "Insulin + DPP4 inhibitors", "None", 
                  "Insulin + GLP-1 RAs", "Biguanides", "Sulfonylureas"))

原绘图代码

# Color palette based on the levels of post_transplant_drug
color_palette <- c(
  "DPP4 inhibitors" = "#4393C3",
  "Insulin + Sulfonylureas" = "#B2182B",
  "Insulin" = "#2166AC",
  "Insulin + DPP4 inhibitors" = "#A6A6A6",
  "None" = "#92C5DE",
  "Insulin + GLP-1 RAs" = "#1B7837",
  "Biguanides" = "#F4A582",
  "Sulfonylureas" = "#D6604D"
)

library(ggalluvial)

ggplot(top_transitions, aes(axis1 = last_pre_tx_drug, 
                            axis2 = post_transplant_drug, y = N)) +
  geom_alluvium(aes(fill = post_transplant_drug), 
                width = 1/25, knot.pos = 0.1, curve_type = "sigmoid") +
  geom_stratum(width = 1/5, fill = "#F0F0F0", color = "#808080") +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), 
                                  size = 3.5, color = "#333333",
            family = "Helvetica Neue", fontface = "bold", 
            hjust = 0.5, vjust = 0.5) +
  scale_x_discrete(limits = c("last_pre_tx_drug", "post_transplant_drug"),
                   labels = c("Last Regimen Used
Pre-Transplant",
                              "First Regimen Used
Post-Transplant"),
                   expand = c(.02, .02)) +
  scale_fill_manual(values = color_palette, name = "Post-Transplant
Drug Class") +
  theme_minimal() +
  theme(plot.title = element_text(face = "bold", size = 16, hjust = 0.5, 
                                  margin = margin(b = 10), 
                                  family = "Helvetica Neue"),
        plot.subtitle = element_text(size = 12, hjust = 0.5, 
                                     margin = margin(b = 20), 
                                     family = "Helvetica Neue"),
        axis.title = element_blank(),
        axis.text = element_text(size = 11, color = "#333333", family = "Helvetica Neue"),
        axis.text.y = element_blank(),
        legend.position = "bottom",
        legend.title = element_text(size = 11, face = "bold", family = "Helvetica Neue"),
        legend.text = element_text(size = 10, family = "Helvetica Neue"),
        panel.grid = element_blank(),
        panel.border = element_blank(),
        plot.margin = margin(20, 20, 20, 20))

问题现象

右侧轴当前顺序与指定的因子层级不符,呈现为镜像第一轴重叠类别的排列,例如实际显示顺序为:Insulin + DPP4 inhibitors、Insulin、Insulin + GLP-1 RAs、None、DPP4 inhibitors、Biguanides、Sulfonylureas、Insulin + Sulfonylureas。

解决方法

ggalluvial默认会优化冲积路径以减少交叉,因此会自动调整第二轴的排序,要强制遵循因子层级,需在绘图代码中添加排序规则:

  • 修改geom_stratum,添加aes(order = after_stat(stratum)),强制按stratum的因子层级排序:
geom_stratum(aes(order = after_stat(stratum)), width = 1/5, fill = "#F0F0F0", color = "#808080")
  • 修改geom_text,同样添加order参数,确保文本与stratum位置对应:
geom_text(stat = "stratum", aes(label = after_stat(stratum), order = after_stat(stratum)), 
          size = 3.5, color = "#333333",
          family = "Helvetica Neue", fontface = "bold", 
          hjust = 0.5, vjust = 0.5)
  • (可选)如果冲积路径的顺序需要对应第二轴的层级,可以在geom_alluvium中添加aes(order = post_transplant_drug),确保填充块的顺序与轴的排序一致:
geom_alluvium(aes(fill = post_transplant_drug, order = post_transplant_drug), 
              width = 1/25, knot.pos = 0.1, curve_type = "sigmoid")

修改后的完整绘图代码

# Color palette based on the levels of post_transplant_drug
color_palette <- c(
  "DPP4 inhibitors" = "#4393C3",
  "Insulin + Sulfonylureas" = "#B2182B",
  "Insulin" = "#2166AC",
  "Insulin + DPP4 inhibitors" = "#A6A6A6",
  "None" = "#92C5DE",
  "Insulin + GLP-1 RAs" = "#1B7837",
  "Biguanides" = "#F4A582",
  "Sulfonylureas" = "#D6604D"
)

library(ggalluvial)

ggplot(top_transitions, aes(axis1 = last_pre_tx_drug, 
                            axis2 = post_transplant_drug, y = N)) +
  geom_alluvium(aes(fill = post_transplant_drug, order = post_transplant_drug), 
                width = 1/25, knot.pos = 0.1, curve_type = "sigmoid") +
  geom_stratum(aes(order = after_stat(stratum)), width = 1/5, fill = "#F0F0F0", color = "#808080") +
  geom_text(stat = "stratum", aes(label = after_stat(stratum), order = after_stat(stratum)), 
                                  size = 3.5, color = "#333333",
            family = "Helvetica Neue", fontface = "bold", 
            hjust = 0.5, vjust = 0.5) +
  scale_x_discrete(limits = c("last_pre_tx_drug", "post_transplant_drug"),
                   labels = c("Last Regimen Used
Pre-Transplant",
                              "First Regimen Used
Post-Transplant"),
                   expand = c(.02, .02)) +
  scale_fill_manual(values = color_palette, name = "Post-Transplant
Drug Class") +
  theme_minimal() +
  theme(plot.title = element_text(face = "bold", size = 16, hjust = 0.5, 
                                  margin = margin(b = 10), 
                                  family = "Helvetica Neue"),
        plot.subtitle = element_text(size = 12, hjust = 0.5, 
                                     margin = margin(b = 20), 
                                     family = "Helvetica Neue"),
        axis.title = element_blank(),
        axis.text = element_text(size = 11, color = "#333333", family = "Helvetica Neue"),
        axis.text.y = element_blank(),
        legend.position = "bottom",
        legend.title = element_text(size = 11, face = "bold", family = "Helvetica Neue"),
        legend.text = element_text(size = 10, family = "Helvetica Neue"),
        panel.grid = element_blank(),
        panel.border = element_blank(),
        plot.margin = margin(20, 20, 20, 20))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 15:44:54