We will here use the ggcompetingrisks1
function in the
Data & Functions
page.
Download
ggcompetingrisks1.R
# Libraries
library(cmprsk)
library(survminer)
library(ggplot2)
# Creation of dataset
set.seed(2)
df <- data.frame(del=rexp(100)*5,
event=sample(c(0, 1, 2),100,replace=TRUE,prob = c(0.3, 0.6, 0.1)),
group=sample(c("A", "B"),100,replace=TRUE))
df$event_surv <- ifelse(df$event==0, 0, 1)
# cuminc object
fit <- cuminc(df$del, df$event)
# surv objet
fit_surv <- survfit(Surv(del, event_surv)~1, data=df)
# !!!! warning !!!! always use survfit with "data="
Using ggcompetingrisks1
:
# We set the xlim, break time and var_time
break.time.by <- 6
var_time <- "del"
xlim <- c(0, 24)
palette <- c("grey")
plot.icc <- ggcompetingrisks1(
fit,
xlab = "Time (months)", ylab="Cumulative incidence",
xlim=xlim, ylim=c(0, 1),
lwd=0.5,
title="", legend="none", legend.title="",
palette=palette,
conf.int = T,
event_suppr = c(1),
ggtheme = theme_classic(),
) + scale_x_continuous(breaks = seq(0, floor(max(df[, var_time], na.rm=T)), break.time.by))
num.icc <- ggrisktable(
fit_surv,
data=df,
xlim=xlim,
break.time.by = break.time.by,
# color = palette, # whole line of number at risk in color
y.text = FALSE,
y.text.col = palette,
fontsize=3,
tables.theme = theme_cleantable()) +
theme(plot.title = element_text(size = 11, color = "black", face = "plain" ))
icc <- ggarrange(plot.icc, num.icc, ncol = 1, nrow = 2, heights = c(0.85, 0.15), align = "v")
icc
Using ggsurvplot
with the same graphic parameters:
plot.surv <- ggsurvplot(fit_surv,
xlab = "Time (months)", ylab="Survival probability",
xlim=xlim, ylim=c(0, 1),
lwd=0.5,
break.time.by = break.time.by,
title="", legend="none", legend.title="",
palette=palette,
conf.int = TRUE,
conf.int.fill = palette,
ggtheme = theme_classic(),
risk.table = T,
risk.table.y.text = FALSE,
risk.table.y.text.col = TRUE,
risk.table.fontsize=3,
tables.theme = theme_cleantable())
plot.surv$table <- plot.surv$table +
theme(plot.title = element_text(size = 11, color = "black", face = "plain" ))
surv <- ggarrange(plot.surv$plot, plot.surv$table, ncol = 1, nrow = 2, heights = c(0.85, 0.15), align = "v")
surv
Combine ggsurvplot
and
ggcompetingrisks
:
This document is a work of the statistics team in the Biostatistics and Medical Information Department at Saint-Louis Hospital in Paris (SBIM).
Developed and updated by Noémie Bigot and Anouk Walter-Petrich
noemie.bigot@aphp.fr; anouk.walter-petrich@u-paris.fr
Based on The R Graph Gallery by Yan Holtz.