BioF3 FigCode · SCI 绘图代码集

冲积图(Alluvial Plot)

导出日期:2026年6月27日

分类:聚类可视化 | 依赖包:ggalluvial、ggplot2、dplyr、tidyr

解决的生物学问题

同一批患者在 3 个随访时间点的疾病状态如何变化?哪些转归路径最常见?

应用场景

输入数据格式

宽表数据框:每行一个个体,每列一个时间点的类别

关键参数

快速开始

1. 直接在线运行

打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。

2. 本地复现

下载脚本和示例数据,本地 RStudio 运行:

# 下载
curl -O https://<your-site>/figcode/scripts/alluvial.R
curl -O https://<your-site>/figcode/data/alluvial.csv

3. 安装依赖

# CRAN 包
install.packages(c("ggalluvial", "ggplot2", "dplyr", "tidyr"))

# Bioconductor 包(如需)
# BiocManager::install(c())

完整代码

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

# --- Demo: 100 患者, 3 个时间点的疾病状态 ---
set.seed(42)
n <- 100
df_wide <- data.frame(
  patient = paste0("P", 1:n),
  T0 = sample(c("Stable","Progressive","Remission"), n, replace = TRUE,
              prob = c(0.5, 0.3, 0.2)),
  T1 = sample(c("Stable","Progressive","Remission"), n, replace = TRUE,
              prob = c(0.4, 0.3, 0.3)),
  T2 = sample(c("Stable","Progressive","Remission","Deceased"), n, replace = TRUE,
              prob = c(0.3, 0.25, 0.3, 0.15))
)

# --- ggalluvial wants long-ish format with frequencies ---
df_count <- df_wide %>%
  count(T0, T1, T2)

# --- Plot ---
ggplot(df_count, aes(axis1 = T0, axis2 = T1, axis3 = T2, y = n)) +
  geom_alluvium(aes(fill = T2), alpha = 0.85) +
  geom_stratum(width = 1/4, fill = "grey95", color = "grey50") +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)),
            size = 3.5) +
  scale_x_discrete(limits = c("Baseline", "3 months", "12 months"),
                   expand = c(0.1, 0.1)) +
  scale_fill_brewer(palette = "Set1") +
  labs(title = "Disease Trajectory (n = 100)",
       y = "Patients", fill = "Final state") +
  theme_minimal()

替换为自己的数据

脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:

延伸阅读