Data & Functions
Competing Risks Curves


Data


We load the packages we will use :

# Libraries
library(cmprsk)
library(survminer)
library(ggplot2)


We create a database :

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)



#Head of dataset
knitr::kable(head(df,8), align = "l")
del event group event_surv
9.3267622 0 A 0
2.0237404 1 A 1
0.7332633 2 B 1
8.6535486 1 B 1
0.4476309 1 A 1
3.3344882 2 B 1
5.3718343 1 B 1
7.5581465 1 A 1

We create the differents survfit objects we will use :

# if one group
fit <- cuminc(df$del, df$event)
fit_surv <- survfit(Surv(del, event_surv)~1, data=df)

# if more than one group
fit_gp <- cuminc(df$del, df$event, df$group)
fit_surv_gp <- survfit(Surv(del, event_surv)~group, data=df)

Download functions


Last update date : 02/08/2024

Download ggcompetingrisks1.R

ggcompetingrisks1 function


This function plots Cumulative Incidence Curves. For cuminc objects it’s a ggplot2 version of plot.cuminc.


ggcompetingrisks1(
fit, an object of a class cmprsk::cuminc - created with cmprsk::cuminc function.
         Warning, the event variable must be without spaces.
gnames, a vector with group names. If not supplied then will be extracted from fit object (cuminc only).
gsep, a separator that extracts group names and event names from gnames object (cuminc only).
multiple_panels, if TRUE then groups will be plotted in different panels (cuminc only).
event_suppr value of events that you don’t want to plot
lwd line thickness
palette the color palette to be used.
         Default is palette from hue_pal
labs character vector specifying legend labels. Used to replace the names of the strata from the fit. Should be given in the same order as those strata.
labs_event character vector specifying legend labels for the events if more than one. Used to replace the names of the events from the fit. Should be given in the same order as those events
type_group if there is more than one group, used to specify if you want to differentiate the groups by different colors (“color”), different line types (“linetype”) or both (“color_linetype”).
         Default is "color"
type_event if there is more than one event, used to specify if you want to differentiate the events by different colors (“color”), different line types (“linetype”) or both (“color_linetype”).
         Default is "linetype".
         If more than one group and more than one event, “color” will be used for type_group and “linetype” will be used for type_event.
alpha see conf.int, scaling actor for the ribbon. The default value is 0.05.
conf.int if TRUE then additional layer (geom_ribbon) is added around the point estimate.
         The ribon is plotted with boundries +- qnorm(1-alpha/2)*standard deviation.
         Standard deviation is estimated with cif.ci method.
         Default is FALSE
legend character specifying legend position. Allowed values are one of c(“top”, “bottom”, “left”, “right”, “none”).
         Default is “top” side position. to remove the legend use legend = “none”.
         Legend position can be also specified using a numeric vector c(x, y).
          In this case it is possible to position the legend inside the plotting area. x and y are the coordinates of the legend box.
          Their values should be between 0 and 1. c(0,0) corresponds to the “bottom left” and c(1,1) corresponds to the “top right” position. For instance use legend = c(0.8, 0.2).
legend.title legend title.
ggtheme function, ggplot2 theme name. Default value is . Allowed values include ggplot2 official themes: see \link[ggplot2]{theme}}. <br>further arguments passed to the function} for customizing the plot.
)

ggcombine function


This function combine ggsurvplot and ggcompeting risks in a same plot.


ggcombine(
list_obj, list of objects of a class ggsurvplot or ggcompetingrisks1.
name_obj, a vector with plot names.
linetype_obj, a vector with plot groups (to differenciate groups by linetype, only for cumulative incidence).
)

ggrisktable function


ggrisktable(
fit, an object of class survfit
data, a dataset used to fit survival curves
risk.table.type, risk table type. Allowed values : c(“absolute”, “percentage”, “abs_pct”, “nrisk_cumcensor”, “nrisk_cumevents”)
survtable, a character string specifying the type of survival table to plot
title, the title of the plot
risk.table.title, the title to be used for the risk table
break.time.by, numeric value controlling time axis breaks. Default value is NULL.
palette, the color palette to be used
color, if “group” : whole line of number at risk in color y.text, logical. Default is TRUE. If FALSE, show bars instead of names in text annotations
y.text.col, logical. Default value is FALSE. If TRUE, the table labels will be colored according to the strata. You can also specify the colors. Only the label or the bar will be colored.
fontsize, text font size
tables.theme, function, ggplot2 theme name (see ggplot2 page)
color, color to be used for the survival curves
legend, legend position. Allowed values are one of c(“top”, “bottom”, “left”, “right”, “none”). If “none” no legend at the top of the risk table.
... )




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.