Basic Competing Risks Curves With 2 Groups or more


We will here use the ggcompetingrisks1 function in the Data & Functions page.
Download ggcompetingrisks1.R

# Libraries
library(cmprsk)
library(survminer)
library(ggplot2)
# Data used
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_gp <- cuminc(df$del, df$event, df$group)
# surv objet 
fit_surv_gp <- survfit(Surv(del, event_surv)~group, data=df) # !!!! warning !!!! always use survfit with "data="



By default, groups will be differentiated by colors and events by line types.

# We set the xlim, break time, var_time and palette
break.time.by <- 6
var_time <- "del"
xlim <- c(0, 24)
palette <- scales::hue_pal()(2)

plot.icc.gp=ggcompetingrisks1(
  fit_gp,                    
  xlab = "Time (months)", 
  xlim=xlim, ylim=c(0, 1), 
  lwd=0.5, 
  title="", legend="top",
  legend.title="",  
  labs=c("Group A", "Group B"),
  labs_event=c("Event 1", "Event 2"),
  palette=palette,
  conf.int = F, multiple_panels = F, type_group="color", type_event="linetype", 
  event_suppr = "",
  ggtheme = theme_classic()                      
) + scale_x_continuous(breaks = seq(0, floor(max(df[, var_time], na.rm=T)), break.time.by))

# !!!! warning !!!! always use survfit with "data="
num.icc.gp <- ggrisktable(
  fit_surv_gp,           
  data=df, 
  xlim=xlim, 
  break.time.by = break.time.by,    
  palette=palette,
  # color = "group", 
  y.text = TRUE,        
  y.text.col = palette,      
  fontsize=3,           
  legend="none",       
  legend.labs=c("Group A", "Group B"),
  tables.theme = theme_cleantable()) +
  theme(plot.title = element_text(size = 11, color = "black", face = "plain" )) 

icc.gp <- ggarrange(plot.icc.gp, num.icc.gp, ncol = 1, nrow = 2, heights = c(0.85, 0.15), align = "v")
icc.gp




Contact

This document is a work of the statistics team in the Biostatistics and Medical Information Department at Saint-Louis Hospital in Paris (SBIM).
Based on The R Graph Gallery by Yan Holtz.