0
votes

A little background that might be useful. I am in a position that I have two plots that need to be combined.

  1. I have used the "survival" package to generate a Kaplan-Meier curve from a set of data.
  2. I have also created and used a custom function to generate survival curves (from the same data, after being processed by WinBUGS)

I generate my K-M curve in the following way.

library("survminer")
library("survival")


KM.est <- survfit(Surv(IPD.data[,1],IPD.data[,2])~1,
              data=IPD.data,type="kaplan-meier")
KM.plot <- ggsurvplot(KM.est, data = IPD.data)

When I now call KM.plot, a plot is generated like I want it. I can also extract just the plot by KM.plot$plot which after checking has the following class:

> class(KM.plot$plot)
[1] "gg"     "ggplot"

https://i.stack.imgur.com/RnSIz.png

Considering now my custom function, (note: nma is a custom object from winbugs)

prepare.survival <- prep_all_survivals(nma, "Study 1", "Treatment")
surv.plot <- survival_plot(prepare.survival)

it also outputs the necessary plot, with

> class(surv.plot)
[1] "gg"     "ggplot"

https://i.stack.imgur.com/yASja.png

My question now is, is it possible to combine both surv.plot and KM.plot in a single figure?

It is hard to recreate the whole project as you will require the WinBUGS models and multiple proprietary functions but I think if you can suggest a way to combine such objects together then I can recreate it. Note that the KM objects can be reproduced via the packages mentioned and the short snipped of data I provide below.

"54" 25.06776181 24 0 1
"55" 25.10061602 23 0 1
"56" 25.62628337 22 0 1
"57" 25.98767967 21 0 1
"58" 26.21765914 20 0 1
"59" 26.41478439 19 1 1
"60" 26.51334702 17 0 1
"61" 26.54620123 16 0 1
"62" 26.67761807 15 0 1
"63" 27.72895277 14 0 1
"64" 28.28747433 13 0 1
"65" 29.24024641 12 0 1
"66" 30.35728953 11 1 1
"67" 31.47433265 9 0 1
"68" 31.96714579 8 0 1
"69" 32.13141684 7 0 1
"70" 33.57700205 6 0 1
"71" 34.98973306 5 1 1
"72" 35.44969199 3 0 1
"73" 36.13963039 2 0 1
"74" 38.47227926 1 0 1

Thank you for your time!

1
Just a guess, can you try KM.plot$plot + surv.plot$layers[[1]]markus
@markus Thanks for your reply! I am getting the following error : Error in FUN(X[[i]], ...) : object 'label' not found I am assuming it is based on inherit.aes in either geom_line or geom_ribbon but this addition to the code does not fix it! Any further tips? Thanks againprophet
Can you give a reproducible example?markus

1 Answers

0
votes

Edit: I just realized you may be asking to combine the curves into a single panel, in which case I think the comment by markus applies.

You should be able to do this using grid.arrange:

library(gridExtra)
grid.arrange(KM.plot$plot, surv.plot)

Or if you want a little more control (i.e. adding labels, captions etc.) you may want to check out cowplot.

cowplot::plot_grid(KM.plot$plot, surv.plot, ncol=1)