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

如何让ggalluvial桑基图跳过缺失访视直接连接有效访视

问题:用ggalluvial绘制跳过失访的桑基图

我需要用ggalluvial包绘制桑基图,展示参与者在历次访视(Visit)中的status变化,参与者的status可在不同状态间切换。部分参与者存在缺失访视的情况,我希望桑基图的流向能跳过这些缺失的访视,直接连接该参与者实际参与的访视。

可复现代码

library(dplyr)
library(ggplot2)
library(ggalluvial)

set.seed(369)
data <- expand.grid(id = 1:100, visit = paste0("Visit", 1:6)) %>%
  slice(-sample(101:600, 50)) %>% # 模拟入组后部分访视缺失的情况
  mutate(status = rbinom(n(), size = 1, prob = 0.2),
         status = ifelse(visit == "Visit1", 0, status), # 所有参与者初始状态为0
         status = factor(status))

ggplot(data, aes(x = visit, stratum = status, alluvium = id, y = 1, 
                     fill = status, color = status)) +
  geom_flow(width = .5, show.legend = F) +
  geom_stratum(width = .5, color = "black", show.legend = T) +
  scale_fill_manual(values=c("cadetblue", "orchid3"))+
  scale_color_manual(values=c("cadetblue", "orchid3"))+
  labs(x = "", y = "Count", fill = "Status", color="Status") +
  theme_minimal()+
  theme(legend.position = "bottom")

当前生成的图

当前桑基图

目标效果示意图

目标桑基图

我曾尝试为缺失访视添加NA值,但会在缺失访视处生成NA分层,流向会进入该分层,不符合需求;也试过Panta Rhei包,它支持跳过分层,但无法生成符合预期的美观图表,若该包能生成类似目标图的高质量结果也可接受。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 10:25:12