Search code examples
rggplot2survival-analysissurvminer

ggplot2/ggsurvplot: Vectorized input to `element_text()` is not officially supported


I'm trying to plot survival curves for a corresponding analysis using survminer & survival packages. Setting risk.table = TRUE in the ggsurvplot command results in a warning saying 'Vectorized input to element_text() is not officially supported. Results may be unexpected or may change in future versions of ggplot2.'

I also tried whether this issue occurs with other data which it does. Here's a simple reprex I ran on R version 4.0.3, Rstudio Version 1.2.5033:

library('survival')
library('survminer')
library('ggplot2')

lung <- survival::lung
fit<- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit, data = lung, risk.table = TRUE)

The warning doesn't occur if risk.table = FALSE which is why I assume that the risktables argument somehow interferes with the ggplot2 x-axis formatting. In a discussion on github about whether vectorized input to element_text() should be supported in the future of ggplot2, developers seem to rather opt for a deprecation cycle https://github.com/tidyverse/ggplot2/issues/3492.

I was wondering if there currently is some way to overcome this issue since I would want to make regular use of ggsurvplot and its risktables feature in the future. The issue also seems to occur for other plots created with ggplot2 (see github discussion above). However, in the reprex, it seems to be related to an inherent argument of ggsurvplot which to me seems critical for future use of this function. Any supportive input would be highly appreciated.


Solution

  • Here is a workaround to get the information from the risk.table feature and present it (and a little more information) next to the survival plot. I guess until the issue is solved by the authors that's the best we can do about it for now.

    library('survival')
    library('survminer')
    library('ggplot2')
    library('kableExtra')
    
    
    # Specify dataset
    lung <- survival::lung
    fit<- survfit(Surv(time, status) ~ sex, data = lung)
    
    
    # -----------Scenario 1: KM-plot and risk.table without writing a function
    
    kmplot <- ggsurvplot(fit, data = lung, risk.table = FALSE)
    risktable <- kmplot$data.survtable
    
    
    # Using R Markdown: Setting results='asis' in the corresponding chunk header - knit to html
    ## Output KM-plot
    kmplot
    
    ## Output risktable
    print (
      kable(risktable, caption = paste("Survival stratified by sex")) %>%
        column_spec(1, bold = T) %>%
        kable_styling(bootstrap_options = "hover", full_width = TRUE))
    
    
    
    # -----------Scenario 2: Using a function
    fun_surv <- function (x) {
    kmplot <- ggsurvplot(x, data = lung, risk.table = FALSE)
    risktable <- kmplot$data.survtable
    list_surv <- list(kmplot, risktable)
    list_surv
    }
    
    res_surv <- fun_surv(fit)
    
    
    # Using R Markdown: Setting results='asis' in the corresponding chunk header - knit to html
    ## Output KM-plot
    res_surv[[1]]
    
    ## Output risktable
    print (
      kable(res_surv [[2]], caption = paste("Survival stratified by sex")) %>%
        column_spec(1, bold = T) %>%
        kable_styling(bootstrap_options = "hover", full_width = TRUE))