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!