This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to lapply ggsurvplot to make survival plots

Hi All,

I have a list of data.frame in R called surv.list and I wanted to use lapply with ggsurvplot to make survival plots from surv.list. I tried something like this:

plot = lapply(surv.list, function(y) {
fit <- survfit(Surv(time, event) ~ Group,
                      data=y)
 ggsurvplot(fit, surv.list,
                      risk.table = TRUE, 
                      pval = TRUE)
})

There is an error Error in eval(inp, data, env) : object 'y' not found. I am new to R and this is the first time I used ggsurvplot so I am not sure if I have used it correctly.

Could someone help? Thanks!

r list lapply ggsurvplot

1 answer

I am not sure what you mean by "list of data.frame", as a data-frame structure is already, technically, a list.

All that you require is a single data-frame with your time, outcome, and test variables. There are also various ways of doing what you want. You can use lapply() at one or more points, but, here, I also use foreach.

This data is taken from my own tutorial, HERE.

1, look at our data

head(survplotdata)
         Time.RFS Distant.RFS gene1 gene2 gene3 gene4 gene5 gene6 gene7 gene8
GSM65752     2280           0   Low   Low   Low   Low   Low   Low   Low   Low
GSM65753     2675           0   Low   Low   Low   Low   Low   Low   Low   Low
GSM65754      426           1   Low  High   Low  High   Low  High   Low  High
GSM65755      182           1   Low   Low   Low   Low   Low   Low   Low   Low
GSM65756       46           1   Low  High   Low  High   Low  High   Low  High
GSM65757     3952           0   Low  High   Low  High   Low  High   Low  High

NB - this data is just 2 genes whose values I have duplicated just for this example

2, create a list of formulae

Here, we can use either foreach or lapply:

foreach

formulae <- list()
genes <- colnames(survplotdata)[3:ncol(survplotdata)]

require(foreach)
foreach(i = 1:length(genes)) %do% {
  formulae[[i]] <- as.formula(paste0("Surv(Time.RFS, Distant.RFS) ~ ", genes[i]))
}

lapply

formulae <- list()
genes <- colnames(survplotdata)[3:ncol(survplotdata)]
formulae <- lapply(genes, function(x) as.formula(paste0("Surv(Time.RFS, Distant.RFS) ~ ", x)))

3, create a list of survival objects

require(survminer)
fits <- surv_fit(formulae, data = survplotdata)

4, create a list of survival plots

p <- ggsurvplot_list(fits,
  data = survplotdata,
  risk.table = FALSE,
  pval = TRUE,
  break.time.by = 500,
  ggtheme = theme_minimal(),
  risk.table.y.text.col = FALSE,
  risk.table.y.text = FALSE)

5, plot all together

arrange_ggsurvplots(p[1:length(p)], print = TRUE, ncol = 4, nrow = 2, title = "Survival plots")

aa

It works! Thanks for breaking down the steps to make it easy to follow.

Log in to answer this question.