【问题标题】:Customizing a competing risks plot in R with package "cmprsk"使用包“cmprsk”在 R 中自定义竞争风险图
【发布时间】:2021-01-19 00:24:49
【问题描述】:

我正在尝试使用 R 和包 cmprsk 自定义竞争风险图。具体来说,我想覆盖默认情况,即竞争事件使用颜色并使用不同的组线型。

这是我的可重现示例:

library(ggplot2)
library(cmprsk)
library(survminer)

# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
                             "status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
                             "Typing" = sample(c("A","B","C","D"), size=50, replace=T))

# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days, 
             fstatus = comp.risk.data$status.tfs, 
             cencode = 0,
             group = comp.risk.data$Typing)

# the default plot makes it impossible to identify the groups
ggcompetingrisks(fit = CR, multiple_panels = F, xlab = "Days", ylab = "Cumulative incidence of event",title = "Competing Risks Analysis")+
  scale_color_manual(name="", values=c("blue","red"), labels=c("Tumor", "Death without tumor"))

使用ggplot_build(),我设法更改了线型和颜色的默认值,但我找不到添加图例的方法。

p2 <- ggcompetingrisks(fit = CR, multiple_panels = FALSE, xlab = "Days", ylab = "Cumulative incidence of event",title = "Death by TCR", ylim = c(0, 1)) +
  scale_color_manual(name="", values=c("blue","red"), labels=c("Tumor", "Death without tumor")) 

q <- ggplot_build(p2)
q$data[[1]]$colour2 <- ifelse(q$data[[1]]$linetype=="solid","blue", ifelse(q$data[[1]]$linetype==22,"red",  ifelse(q$data[[1]]$linetype==42,"green",  ifelse(q$data[[1]]$linetype==44,"black", NA))))
q$data[[1]]$linetype <- ifelse(q$data[[1]]$colour=="blue","solid", ifelse(q$data[[1]]$colour=="red","dashed", NA))
q$data[[1]]$colour <- q$data[[1]]$colour2

q$plot <- q$plot + ggtitle("Competing Risks Analysis") + guides(col = guide_legend()) + theme(legend.position = "right")


p2 <- ggplot_gtable(q)
plot(p2)

有谁知道如何将图例添加到由ggplot_build() 操纵的情节中?或者另一种绘制竞争风险的方法,例如颜色表示组,线型表示事件?

【问题讨论】:

  • 不是通过 ggplot_build 来操作绘图,而是通过构建和调整 survminer::ggcompetingrisks 或更具体地说是 survminer:::ggcompetingrisks.cuminc 的源代码来编写自己的自定义绘图函数不是更容易吗?跨度>
  • @stefan 这可能是矫枉过正。在函数产生的 ggplot 对象中交换美学映射是相当简单的。我想您可以为此编写一个包装器,但我认为我不会打扰 - 见下文。
  • 这个问题可以作为提交关于 SO 问题的正确方法的一个很好的例子。

标签: r ggplot2 survival-analysis


【解决方案1】:

您无需走ggplot_build 路线。 ggcompetingrisks 函数返回一个 ggplot 对象,该对象本身包含美学映射。你可以用aes覆盖这些:

p <- ggcompetingrisks(fit = CR, 
                 multiple_panels = F, 
                 xlab = "Days", 
                 ylab = "Cumulative incidence of event",
                 title = "Competing Risks Analysis") 

p$mapping <- aes(x = time, y = est, colour = group, linetype = event)

现在我们已经反转了线型和颜色美学映射,我们只需要交换图例标签就可以了:

p + labs(linetype = "event", colour = "group")

请注意,您还可以像任何其他 ggplot 对象一样向 p 添加色标、主题、坐标变换。

【讨论】:

  • 海杰艾伦。好工作。感谢您指出这一点并教会了我一些新东西。我牢记这一点。最佳 S.
  • 嗯,我喜欢它。如此快速和容易。不后悔走ggplot_build 路线,因为这是一次很好的学习经历——但知道这会为我节省很多时间:)
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 2011-03-25
相关资源
最近更新 更多