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

如何修改ggcompetingrisks默认图例为分组-状态组合形式?

竞争风险图图例与线型调整需求

随机数据的双事件竞争风险图

示例绘图代码

set.seed(10) 
ftime <- runif(125, min = 0.1, max = 10)
fstatus <- factor(sample(rep(0:2, times = round(c(74/125, 40/125, 11/125) * 125)),
                         size = 125, replace = TRUE))
group <- factor(sample(rep(0:1, times = round(c(107/125, 18/125) * 125)),
                       size = 125, replace = TRUE))

cm <- cuminc(ftime = ftime, fstatus = fstatus, group = group)

ggcr <- ggcompetingrisks(cm,
                         censor = FALSE,
                         conf.int = FALSE,
                         multiple_panels = FALSE,
                         gnames = c("CIR Event1", "CIR Event2", "NRM Event1", "NRM Event2"),
                         xlim = c(0,10),
                         ylim = c(0, 1.0),
                         xlab = "Time (Years)",
                         ylab = "Cum. Risk",
                         title = "",
                         legend.title = "",
                         legend.fond = 16,
                         palette = c("red3", "royalblue3")
)


CIR_plot <- ggpar(ggcr + geom_line(linewidth = 1) + 
                    scale_y_continuous(label = c("0,00", "0,25", "0,50", "0,75", "1,00"),
                                       breaks = c(0.00, 0.25, 0.50, 0.75, 1.0)) +
                    scale_x_continuous(breaks = 0:10),
                  font.main = c(14),
                  font.x = c(14),
                  font.y = c(14),
                  font.caption = c(14), 
                  font.legend = c(12), 
                  font.tickslab = c(12))

需求描述

我需要将图例修改为:Event1 CIR(红色实线)、Event1 NRM(红色虚线)、Event2 CIR(蓝色实线)、Event2 NRM(蓝色虚线)。由于是工作修订需求,希望尽量少改动现有绘图代码,但目前在ggcompetingrisks/ggplot中没找到可行方案,求易于实现的解决办法?


解决方案

只需两处小改动即可实现需求,完全保留原有代码核心逻辑:

  1. 调整图例文本:
    将ggcompetingrisks中的gnames参数替换为目标图例名称:

    gnames = c("Event1 CIR", "Event2 CIR", "Event1 NRM", "Event2 NRM")
    
  2. 指定线型并同步图例:
    在geom_line中添加线型参数,同时新增scale_linetype_identity让图例正确显示线型:

    CIR_plot <- ggpar(ggcr + 
                        geom_line(linewidth = 1, linetype = c(1,1,2,2)) + 
                        scale_y_continuous(label = c("0,00", "0,25", "0,50", "0,75", "1,00"),
                                           breaks = c(0.00, 0.25, 0.50, 0.75, 1.0)) +
                        scale_x_continuous(breaks = 0:10) +
                        scale_linetype_identity(guide = "legend"),
                      font.main = c(14),
                      font.x = c(14),
                      font.y = c(14),
                      font.caption = c(14), 
                      font.legend = c(12), 
                      font.tickslab = c(12))
    

最终会得到完全符合需求的图例与线型:

  • Event1 CIR:红色实线
  • Event2 CIR:蓝色实线
  • Event1 NRM:红色虚线
  • Event2 NRM:蓝色虚线

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 18:05:56