1

I've found this example of a plot for competing risk analysis: Customizing a competing risks plot in R with package "cmprsk". The version of Allan Cameron works really great, but unfortunately I can't edit the legend labels for the grouping variable - i.e. the different line colors.

# Code written by Allan Cameron in the post mentioned above (slightly edited with my label names)

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)

p <- ggcompetingrisks(fit = CR, 
                      multiple_panels = F, 
                      xlab = "Days", 
                      ylab = "Cumulative incidence of event",
                      title = "Competing Risks Analysis") +
  scale_linetype_manual(name="my genotype",values=c(1, 2),labels=c("yes","no"))

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

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

I was able to edit the labels for the different line types (dotted or not, i.e. "yes", "no"), but I can't change the values "A", "B", "C", "D" to maybe "one", "two", "three", "four".

Can anyone help me?

Thank you!

MDStat
  • 355
  • 2
  • 17

1 Answers1

1

Basically it's the same approach as for the linetype. To change just the labels you could set them via the labels argument of scale_color_discrete or if you additionally like to change the colors too, I would suggest to make use of scale_color_manual instead:

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

p <- ggcompetingrisks(fit = CR, 
                      multiple_panels = F, 
                      xlab = "Days", 
                      ylab = "Cumulative incidence of event",
                      title = "Competing Risks Analysis") +
  scale_linetype_manual(name="my genotype",values=c(1, 2),labels=c("yes","no")) +
  scale_color_discrete(labels = c(A = "one", B = "two", C = "three", D = "four"))

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

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

stefan
  • 90,330
  • 6
  • 25
  • 51
  • That is great - thank you so much! Another question: Could you maybe explain the last two lines of code to me (p$mapping... and p + labs...? Where does the "event" come from which is assigned to linetype? – MDStat Sep 02 '21 at 12:36
  • `p$mapping <- aes()` is used to change the default mapping used by `ggcompetingrisks`. That was the issue in the question you linked. The last line simply sets the labels used in the legends. `event` itself is as far as I get an internal variable computed by `ggcompetingrisks`. – stefan Sep 02 '21 at 12:46