Search code examples
rsurvival-analysisgridextra

grid.arrange (Gridextra) Error when combining survival curves stored in a list


Slight variations of this question have popped up a lot, but none seem to help me with my plots.

I am doing survival analysis conditionnal on the moment of entry in a given program (more precisely, survival in unemployment after entry in training).

I have a list of 12 databases (for people entering the program at month 1 to 12) and I am doing 12 Kaplan Meier survival curves (based on those 12 databases) also stored in a list.

When I try to put the 12 Kaplan Meier curves together in a grid using grid.arrange, I get the following error "Error in gList(list(x = c(47.0657534246575, 47.0657534246575), y = c(0.262116178126773, : only 'grobs' allowed in "gList"".

Where is the problem? Are there alternatives?

library(survival)
library(survminer)
library(gridExtra)

#list containing the 12 databases
list_data_month

#Kaplan Meier estimates + curves for the 12 months
month <- c(1:12)
list_kp_month <- list() #list for Kaplan Meier estimates
list_kp_plot_month <- list() #list for Kaplan Meier curves

for(i in month){
 
 #Kaplan Meier estimates
 list_kp_month[[i]] <- survfit(Surv(time=time_t,event=status)~treatment, data=list_data_month[[i]],robust =TRUE,weights =weights)
 
 #Kaplan Meier curves
 list_kp_plot_month[[i]] <- ggsurvplot(fit = list_kp_month[[i]],
                                          palette = NULL, # Couleur, e.g. "blue"
                                          linetype = 1, # Ligne "solide"
                                          surv.median.line = "none", # Essayer "hv"
                                          conf.int = TRUE,
                                          risk.table = FALSE, # Peut être "TRUE"
                                          cumevents = FALSE,
                                          cumcensor = FALSE,
                                          tables.height = 0.25,
                                          xlab = "Months",
                                          legend = "bottom",
                                          legend.title = "",
                                          legend.labs = c("Control", "Treatment")
  )
}

#Gridextra - Combining the 12 Kaplan Meier curves together
n <- length(list_kp_plot_month)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(list_kp_plot_month, ncol=nCol))

Solution

  • The issue is that the object returned by ggsurvplot isn't a ggplot object. It's an object of class ggsurvplot which is simply a list. The plot itself is just one element of this list with name plot.

    Hence, to solve your issue you have to first extract the plots from your list_kp_plot_month list using e.g. lapply(list_kp_plot_month, [[, "plot")

    Making use of a simplified example using the lung dataset:

    library(survival)
    library(survminer)
    library(gridExtra)
    
    # Create example data. Just 2 months
    list_data_month <- list(month1 = lung, month2 = lung)
    
    list_kp_month <- list()
    list_kp_plot_month <- list()
    
    for (i in seq_along(list_data_month)) {
      # Kaplan Meier estimates
      list_kp_month[[i]] <- survfit(Surv(time = time, event = status) ~ sex, data = list_data_month[[i]], robust = TRUE)
      # Kaplan Meier curves
      list_kp_plot_month[[i]] <- ggsurvplot(
        fit = list_kp_month[[i]],
        palette = NULL, # Couleur, e.g. "blue"
        linetype = 1, # Ligne "solide"
        surv.median.line = "none", # Essayer "hv"
        conf.int = TRUE,
        risk.table = FALSE, # Peut être "TRUE"
        cumevents = FALSE,
        cumcensor = FALSE,
        tables.height = 0.25,
        xlab = "Months",
        legend = "bottom",
        legend.title = "",
        legend.labs = c("Control", "Treatment")
      )
    }
    
    # Gridextra - Combining the 12 Kaplan Meier curves together
    n <- length(list_kp_plot_month)
    nCol <- floor(sqrt(n))
    
    # Get plots from ggsurvplot objects
    plots <- lapply(list_kp_plot_month, `[[`, "plot")
    
    do.call("grid.arrange", c(plots, ncol = nCol))