rsurvminer

Produce a facet with an overall group in ggsurvplot in R


In R, I am using ggsurvplot_facet to produce survival curves plotted for groups sex as a facet stratified by a variable ecog. However, I would like to have an overall group as well in the same facet as well. Is this possible?

ggsurvplot_add_all did not help.

Here is some example data:

library(survminer)
lung$ecog <- ifelse(lung$ph.ecog == 0, 0, 1)
fit <- surv_fit(Surv(time, status) ~ sex, data = lung)

fig_os <- ggsurvplot_facet(fit, data = lung, facet.by = 'ecog')

Enter image description here

I need a survival curve for the whole population, independent of ecog.


Solution

  • Cheat a little bit, by row-binding lung to a copy of lung where in the latter ecog has been replaced with a constant.

    1. Cheat, making a copy, setting ecog to 2, row-binding, and changing ecog in the row-bound dataset to a factor.

      lung2 <- copy(lung)
      lung2$ecog <- 2
      lung2 <- rbind(lung,lung2)
      lung2$ecog <- factor(lung2$ecog,labels = c("0", "1", "Overall"))
      
    2. Then use your code above, but using lung2 as the dataset.

      fit <- surv_fit(Surv(time, status) ~ sex, data = lung2)
      fig_os <- ggsurvplot_facet(fit, data = lung2, facet.by = 'ecog')
      

    Output:

    ecog