I would like to plot a kaplan meier curve (KM) and cumulative events or cumulative incidence function (CIF) in one plot as a lattice.
I have switched recently from SAS to R, and in SAS you can do it all in one step using a macro (See this image), but I couldn't find something similar in R yet.
Currently, I run a code for two separate graphs. The first plots survfit
object using ggsurvplot
which results in a KM curve, While the second plots a cuminc
object after a number of transformations using ggplot
. ggcompetingrisks
was not very optimizable, so I don't use it. Also I am interested in plotting one certain competing risk for example death from cancer, and not all competing risks.
Here is an example of my current code using the BMT data-frame from the survminer
package.
library(survminer)
library(cmprsk)
data(BMT)
# I'll add the variable Death to plot overall survival.
BMT <- mutate(BMT, death = ifelse (status == 1, 1, 0))
# KM plot:
figKM <- ggsurvplot(survfit(Surv(ftime, death) ~ dis, BMT))
figKM
# CIF plot:
cif <- cuminc(ftime = BMT$ftime, fstatus = BMT$status, group = BMT$dis, cencode = 0)
cifDT <- cif %>%
list_modify("Tests" = NULL) %>%
map_df(`[`, c("time", "est"), .id = "id") %>%
filter(id %in% c("0 1","1 1")) # to keep the incident I want
figCIF <- ggplot (cifDT, aes(x = time, y = est, color = id)) + geom_step(lwd = 1.2)
figCIF
is there a way to put figKM and figCIF together in a lattice plot? May by plotting them differently?