kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
495 stars 160 forks source link

Tiling multiple ggsurvplots into layout/matrix view #262

Closed shraddhapai closed 6 years ago

shraddhapai commented 6 years ago

Hi,

I have multiple ggsurvplot objects that I would like to tile. For ggplot2 I normally use multiplot (from here: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/) to lay out the graphs in a matrix format. However, running multiplot() on ggsurvplot objects does not tile these; rather it uses a new window for each image. Could you please suggest a way to achieve the tiling? I have 12 conditions for 4 tumours that I need to display in this format and copy-pasting graphs into a photo editor would be tedious.

Thanks! Shraddha Shraddha Pai Post-doctoral Research Fellow, http://baderlab.org University of Toronto

Steps to reproduce the problem

library(survival)
library(survminer)
data(lung)

# Compute survival curves
fit1 <- survfit(Surv(time, status) ~ ph.ecog, data = lung)
fit2 <- survfit(Surv(time, status) ~sex, data = lung)

source("multiplot.R")  # from link above

# does not work for ggsurvplot objects
>plotList <- list(x=ggsurvplot(fit1, data=lung),y=ggsurvplot(fit2,data=lung))
>multiplot(plotlist=plotList, cols=2)

screen shot 2017-11-06 at 12 56 53 pm

screen shot 2017-11-06 at 12 55 19 pm

# does work for ggplot objects
> other <- list()
> other[[1]] <- ggplot(lung,aes(x=time,y=meal.cal))+geom_point()
> other[[2]] <- ggplot(lung,aes(x=time,y=pat.karno))+geom_point()
> multiplot(plotlist=other,cols=2)

screen shot 2017-11-06 at 12 53 34 pm

session_info()

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
 [1] bindrcpp_0.2    survminer_0.4.0 ggpubr_0.1.5    magrittr_1.5
 [5] rms_5.1-1       SparseM_1.77    Hmisc_4.0-3     ggplot2_2.2.1
 [9] Formula_1.2-2   survival_2.41-3 lattice_0.20-35

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12        mvtnorm_1.0-6       tidyr_0.7.0
 [4] zoo_1.8-0           assertthat_0.2.0    digest_0.6.12
 [7] psych_1.7.5         R6_2.2.2            plyr_1.8.4
[10] backports_1.1.0     acepack_1.4.1       MatrixModels_0.4-1
[13] rlang_0.1.2         lazyeval_0.2.0      multcomp_1.4-6
[16] data.table_1.10.4   rpart_4.1-11        Matrix_1.2-11
[19] checkmate_1.8.3     labeling_0.3        splines_3.3.1
[22] stringr_1.2.0       foreign_0.8-69      htmlwidgets_0.9
[25] munsell_0.4.3       broom_0.4.2         pkgconfig_2.0.1
[28] base64enc_0.1-3     mnormt_1.5-5        htmltools_0.3.6
[31] nnet_7.3-12         km.ci_0.5-2         tibble_1.3.3
[34] gridExtra_2.2.1     htmlTable_1.9       codetools_0.2-15
[37] dplyr_0.7.2         MASS_7.3-47         xtable_1.8-2
[40] nlme_3.1-131        polspline_1.1.12    gtable_0.2.0
[43] KMsurv_0.1-5        scales_0.4.1        stringi_1.1.5
[46] reshape2_1.4.2      latticeExtra_0.6-28 survMisc_0.5.4
[49] sandwich_2.4-0      TH.data_1.0-8       RColorBrewer_1.1-2
[52] tools_3.3.1         cmprsk_2.2-7        glue_1.1.1
[55] purrr_0.2.3         parallel_3.3.1      colorspace_1.3-2
[58] cluster_2.0.6       knitr_1.17          bindr_0.1
[61] quantreg_5.33
kassambara commented 6 years ago

Use this:

ggsurvlist <- list(
  x = ggsurvplot(fit1, data=lung),
  y = ggsurvplot(fit2,data=lung)
  )

# Arrange multiple ggsurvplots and print the output
arrange_ggsurvplots(ggsurvlist, print = TRUE, ncol = 2, nrow = 1)

# Arrange and save into pdf file
res <- arrange_ggsurvplots(ggsurvlist, print = FALSE)
ggsave("myfile.pdf", res)
shraddhapai commented 6 years ago

Perfect, thank you. That resolves my issue. Didn't realize there was a built-in function because I use a third-party script to achieve this in ggplot2. Regards,Shraddha

kassambara commented 6 years ago

great! so we can close this issue