Dynamic legend labels for ggsurvplot

543 views Asked by At

I have a general question related to dynamic labels and titles for ggsurvplot. Is it possible to provide dynamic legend labels and legend title to the ggsurvplot function when using it in nested list using map from package purrr?

  • The dataset I used is the NCCTG Lung Cancer Data lung{survival} and slightly adapted include ECOG scores below 3

  • The code below dynamically creates survival plots for each of the prognostic factors (sex and ECOG score, defined in the varnames). However, I can't figure out how to add dynamic title,legend.labs and legend.title to the arguments within the ggsurvplot function as they need to be provided as a character vector that should change with each varname (sex and ph.ecog).

plotspreview

Any help would be greatly appreciated.

Thanks!

HTJ

varnames <- c("sex", "ph.ecog")

lung <- lung %>% filter(ph.ecog<3)

res.tabs <- lung  %>%
  gather(key=VAR, value, varnames) %>%
  nest( data = -VAR) %>% mutate(
    fit = map(data, ~ coxph(Surv(time, status) ~  value, data = .x)),
    survfit = map(data, ~ survfit(Surv(time, status) ~ value , data = .x, conf.type ="log")),
    plots = ggsurvplot(survfit, data, conf.int = TRUE, title = "???", legend.labs = "???", legend.title = "???"))
0

There are 0 answers