Customizing a competing risks plot in R with package "cmprsk"
Asked Answered
I

1

5

I am trying to customize a plot for competing risks using R and the package cmprsk. Specifically, I want to overwrite the default that for competing events colors are used and for different groups linetypes are used.

Here is my reproducible example:

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"))

enter image description here

Using ggplot_build() I managed to change the default regarding linetype and color, but I cannot find a way to add a legend.

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)

enter image description here

Does anyone know how to add the legend to a plot manipulated by ggplot_build()? Or an alternative way to plot the competing risks such that color indicated group and linetype indicates event?

Inebriate answered 3/10, 2020 at 16:35 Comment(3)
Instead of manipulating the plot via ggplot_build, wouldn't it be easier to write your own custom plotting function by building on and adjusting the source code of survminer::ggcompetingrisks or more specifically survminer:::ggcompetingrisks.cuminc?Galyak
@Galyak that's probably overkill. It's fairly trivial to swap the aesthetic mappings in the ggplot object the function produces. I guess you could write a wrapper around this, but I don't think I'd bother - see below.Wifeless
This question could serve as a good example of the right way to submit a question on SO.Stigmatize
W
6

You don't need to go down the ggplot_build route. The function ggcompetingrisks returns a ggplot object, which itself contains the aesthetic mappings. You can overwrite these with 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)

Now we have reversed the linetype and color aesthetic mappings, we just need to swap the legend labels and we're good to go:

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

enter image description here

Note that you can also add color scales, themes, coordinate transforms to p like any other ggplot object.

Wifeless answered 3/10, 2020 at 16:59 Comment(2)
Hej Allan. Good job. And thanks for pointing that out and for teaching me something new. I keep that in mind. Best S.Galyak
uh, I love it. So fast and easy. No regrets going down the ggplot_build route since it was a good learning experience - but knowing this would have saved me a lot of time :)Inebriate

© 2022 - 2024 — McMap. All rights reserved.