Search code examples
rggplot2survival-analysis

How can I overlay two ggplot2 objects from contsurvplot package?


I have two plots that are generated from the plot_surv_at_t from the constsurvplot package. One is looking at the 5-year recurrence free survival versus tumor size. The other is cancer specific survival versus tumor size.

model1 <- coxph(Surv(Time_To_Recurrence,Recurrence) ~ Tumor_Size, data = data, x=TRUE)
model2 <- coxph(Surv(CSS,Cancer_Death) ~ Tumor_Size, data = data, x=TRUE)

plot_surv_at_t(time = "Time_To_Recurrence",
               status = "Recurrence", 
               variable = "Tumor_Size",
               data = data1, 
               model = model1, 
               t=60)

plot_surv_at_t(time = "CSS",
               status = "Cancer_Death", 
               variable = "Tumor_Size",
               data = data1, 
               model = model2, 
               t=60)

Each individual plot looks great, but I want to show the impact of size on recurrence free and cancer specific survival in the same plot. I've tried the patchwork package and am able to get them next to each other, but I have not been able to onlay them.

Thanks


Solution

  • One potential option is to pull the data from the second ggplot2 object and add it to the first ggplot2 object, e.g.

    library(survival)
    library(survminer)
    #> Loading required package: ggplot2
    #> Loading required package: ggpubr
    #> 
    #> Attaching package: 'survminer'
    #> The following object is masked from 'package:survival':
    #> 
    #>     myeloma
    #options(timeout = 6000)
    #install.packages("contsurvplot", dependencies = TRUE)
    library(contsurvplot)
    
    fit <- coxph(Surv(time, status) ~ age, data = lung, x = TRUE)
    plot1 <- plot_surv_at_t(time = "time",
                   status = "status", 
                   variable = "age",
                   data = lung, 
                   model = fit, 
                   t=60)
    
    plot1
    

    
    lung2 <- lung %>%
      mutate(CSS = sample(1:1000, 228))
    fit2 <- coxph(Surv(CSS, status) ~ age, data = lung2, x = TRUE)
    plot2 <- plot_surv_at_t(time = "CSS",
                            status = "status", 
                            variable = "age",
                            data = lung2, 
                            model = fit2,
                            t=60)
    plot2
    

    
    plot1 +
      geom_line(data = plot2$data,
                aes(x = cont, y = est),
                color = "red")
    

    Created on 2023-03-19 with reprex v2.0.2