rsurvival-analysis

Cumulative incidence plot in R using ggsurvplot


I am trying to create a cumulative incidence plot that also gives me a risktable at specified time points, has confidence intervalls and censor marks. The structure of my data is like this:

library(survival)
library(tidyverse)
library(ggsurvfit)
library(survminer)
library(tidycmprsk)
library(prodlim)
library(cmprsk)

time = c(1:10)
status = c(T, F, T, F, T, F, T, F, T, F)
df <- data.frame(time, status)

Censor time is in days and in status T = event, F = no event. The original data also includes NAs.

I almost get the result that i want with this:

ci_surv <- Surv(time, status)
ci_survfit <- survfit2(ci_surv ~ 1)

ggsurvfit(ci_survfit) +
  labs(
    x = "Days",
    y = "Event-free proportion"
  ) +
  scale_x_continuous(breaks = c(2, 5, 10)) +
  add_confidence_interval() +
  add_risktable() +
  add_censor_mark()

But obviously instead of the event-free proprtion (survival) I would like to get the cumulative incidence displayed on the y-axis.

I tried several of other solutions already. One gives me a correct plot:

plot(ci_survfit,
     fun = function(Surv) 1-Surv,
     atrisk.title="Number at risk",
     atrisk=T)

but this gives me "not a graphical parameter" error for basically anything that I want to add to the plot like atrisk as above.

The same is true for this solution:

ci_cuminc <- cmprsk::cuminc(time, status)
plot(ci_cuminc, atrisk = T)

where atrisk also doesn´t work. Appreciate your help!


Solution

  • You can use type = "risk" in ggsurvfit

    ggsurvfit(ci_survfit, type = "risk") +
      labs(
        x = "Days",
        y = "Event-free proportion"
      ) +
      scale_x_continuous(breaks = c(2, 5, 10)) +
      add_confidence_interval() +
      add_risktable() +
      add_censor_mark()
    

    enter image description here

    Or you can use ggcuminc from the ggsurvfit package, which is specifically for plotting cumulative incidence:

    library(tidycmprsk)
    library(ggsurvfit)
    
    time = c(1:10)
    status = c(T, F, T, F, T, F, T, F, T, F)
    df <- data.frame(time, status)
    
    df$status <- factor(df$status)
    
    cuminc(Surv(time, status) ~ 1, data = df) %>%
      ggcuminc(outcome = 'TRUE') +
      labs(
        x = "Days",
        y = "Cumulative incidence"
      ) +
      scale_x_continuous(breaks = c(2, 5, 10)) +
      add_confidence_interval() +
      add_risktable() +
      add_censor_mark()
    

    enter image description here