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

如何调整ggcuminc图中两类竞争结局的线条颜色与线型?

解决ggcuminc图竞争结局线条颜色与线型修改失效问题

问题背景

你需要为无分组的整体队列中,「癌症死亡」(设为红色实线)和「其他原因死亡」(设为蓝色实线)两类竞争结局调整ggcuminc图的线条样式,但当前代码未实现预期效果。

问题原因

原代码中scale_color_manual未明确对应结局名称,且未指定线型映射。ggcuminc默认会将结局同时映射到color和linetype美学属性,因此需要同时指定颜色和线型的对应关系,确保与目标结局匹配。

修正后的代码

library(ggplot2) 
library(tidycmprsk) 
library(ggsurvfit)

# 定义对应结局的颜色和线型,名称要与结局完全匹配
custom_colors <- c("death from cancer" = "red", "death other causes" = "blue")
custom_linetypes <- c("death from cancer" = "solid", "death other causes" = "solid")

cuminc(Surv(ttdeath, death_cr) ~ 1, trial) %>%
  ggcuminc(outcome = c("death from cancer","death other causes")) +
  add_confidence_interval() +
  add_risktable(size = 4, theme = theme_risktable_default(axis.text.y.size = 12, plot.title.size = 12)) +
  theme(axis.title = element_text(size = 20), 
        axis.text = element_text(size = 15), 
        legend.text = element_text(size = 15)) +
  scale_color_manual(values = custom_colors) + # 按结局名称映射颜色
  scale_linetype_manual(values = custom_linetypes) # 按结局名称映射线型

关键说明

  • 必须确保custom_colors和custom_linetypes的名称与outcome参数中的结局名称完全一致,这样才能精准匹配对应线条的样式;
  • 如果只需要修改颜色,也可以保留名称映射,避免因结局顺序变化导致颜色匹配错误;
  • 若要调整线型,只需修改custom_linetypes中的值(如"dashed"为虚线、"dotted"为点线等)即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 15:10:19