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

如何调整ggsurvfit包ggcuminc图中风险表的标签顺序?

解决ggcuminc风险表分组顺序问题

针对你遇到的CIF图风险表顺序异常的问题,可以通过以下几种方法调整:

方法1:直接指定风险表的分组顺序

在add_risktable()中使用order参数强制设置分组显示顺序,这是最直接的解决方案:

cuminc(Surv(LC_from_local_treatment_per_tumor, Loss_of_local_control) ~ Treatment_type_tumor_near_gallbladder, efficacy_df_CIF) %>%
  ggcuminc(outcome = c("Event", "Competing Event")) +
  scale_color_manual(values = c("#A81D24", "#FFD700")) +
  # 指定风险表分组从上到下的顺序
  add_risktable(order = c("Resection", "MWA")) +
  coord_cartesian(ylim = c(0, 1)) +
  add_censor_mark(size = 2, alpha = 0.2) +
  scale_ggsurvfit() +
  labs(title = "CIF for Local Control (LC) per tumor") +
  theme(plot.title = element_text(hjust = 0.5)) +
  add_pvalue(caption = "Gray's Test (Event) p={round(glance(cif_LC)$p.value_1, digits=3)}")

方法2:调整cuminc对象的分组因子水平

先生成cuminc对象,手动重置分组的因子水平,确保顺序符合需求:

# 生成竞争风险模型对象
cif_obj <- cuminc(
  Surv(LC_from_local_treatment_per_tumor, Loss_of_local_control) ~ Treatment_type_tumor_near_gallbladder, 
  data = efficacy_df_CIF
)
# 重置分组因子的显示顺序
cif_obj$group <- factor(cif_obj$group, levels = c("Resection", "MWA"))

# 绘制CIF图
cif_obj %>%
  ggcuminc(outcome = c("Event", "Competing Event")) +
  scale_color_manual(values = c("#A81D24", "#FFD700")) +
  add_risktable() +
  coord_cartesian(ylim = c(0, 1)) +
  add_censor_mark(size = 2, alpha = 0.2) +
  scale_ggsurvfit() +
  labs(title = "CIF for Local Control (LC) per tumor") +
  theme(plot.title = element_text(hjust = 0.5)) +
  add_pvalue(caption = "Gray's Test (Event) p={round(glance(cif_obj)$p.value_1, digits=3)}")

方法3:通过颜色映射同步顺序

使用scale_color_manual()的limits参数强制颜色映射的顺序,同步风险表的分组顺序:

cuminc(Surv(LC_from_local_treatment_per_tumor, Loss_of_local_control) ~ Treatment_type_tumor_near_gallbladder, efficacy_df_CIF) %>%
  ggcuminc(outcome = c("Event", "Competing Event")) +
  # 强制颜色映射的分组顺序,同步风险表显示
  scale_color_manual(values = c("#A81D24", "#FFD700"), limits = c("Resection", "MWA")) +
  add_risktable() +
  coord_cartesian(ylim = c(0, 1)) +
  add_censor_mark(size = 2, alpha = 0.2) +
  scale_ggsurvfit() +
  labs(title = "CIF for Local Control (LC) per tumor") +
  theme(plot.title = element_text(hjust = 0.5)) +
  add_pvalue(caption = "Gray's Test (Event) p={round(glance(cif_LC)$p.value_1, digits=3)}")

差异原因说明

survfit2()会严格继承数据集中的因子水平顺序,但cuminc()在计算竞争风险模型时,可能会默认按字母顺序重新排列分组,导致风险表顺序与原始数据不一致,上述方法均可覆盖这种默认行为。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:49:55