kassambara / survminer

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

ggsave does not work with ggsurvplot objects anymore #544

Open bipoff opened 3 years ago

bipoff commented 3 years ago

Hi,

first of all, thank you very much for providing us with this invaluable package! I used it for two years now, but now there is below issue coming up.

Thanks a lot!

Expected behavior

ggsave should be ble to save ggsurvplots with risk tables. I use plot = print(plot, newpage = F). Still it does not work as it used to work ealier.

Actual behavior

the saved pdf is empty

Steps to reproduce the problem

library(survival) library(survminer) library(tidyverse) library(Cairo) fit<- survfit(Surv(time, status) ~ sex, data = lung) plot <- ggsurvplot(fit, data = lung, risk.table = T) plot ggsave("plot.pdf", plot = print(plot, newpage = F), device = cairo_pdf)

session_info()

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

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

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

other attached packages:
 [1] Cairo_1.5-12.2  forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4    
 [6] readr_1.4.0     tidyr_1.1.3     tibble_3.1.2    tidyverse_1.3.1 survminer_0.4.9
[11] ggpubr_0.4.0    ggplot2_3.3.5   survival_3.2-11

loaded via a namespace (and not attached):
 [1] httr_1.4.2        jsonlite_1.7.2    splines_4.1.0     carData_3.0-4    
 [5] modelr_0.1.8      assertthat_0.2.1  cellranger_1.1.0  pillar_1.6.1     
 [9] backports_1.2.1   lattice_0.20-44   glue_1.4.2        digest_0.6.27    
[13] ggsignif_0.6.2    gridtext_0.1.4    rvest_1.0.0       colorspace_2.0-2 
[17] Matrix_1.3-3      pkgconfig_2.0.3   broom_0.7.8       haven_2.4.1      
[21] xtable_1.8-4      scales_1.1.1      km.ci_0.5-2       openxlsx_4.2.4   
[25] rio_0.5.27        KMsurv_0.1-5      generics_0.1.0    farver_2.1.0     
[29] car_3.0-11        ellipsis_0.3.2    withr_2.4.2       cli_3.0.0        
[33] magrittr_2.0.1    crayon_1.4.1      readxl_1.3.1      ggtext_0.1.1     
[37] fs_1.5.0          fansi_0.5.0       rstatix_0.7.0     xml2_1.3.2       
[41] foreign_0.8-81    tools_4.1.0       data.table_1.14.0 hms_1.1.0        
[45] lifecycle_1.0.0   munsell_0.5.0     reprex_2.0.0      zip_2.2.0        
[49] compiler_4.1.0    rlang_0.4.11      grid_4.1.0        rstudioapi_0.13  
[53] labeling_0.4.2    gtable_0.3.0      abind_1.4-5       DBI_1.1.1        
[57] curl_4.3.2        markdown_1.1      R6_2.5.0          gridExtra_2.3    
[61] zoo_1.8-9         lubridate_1.7.10  knitr_1.33        survMisc_0.5.5   
[65] utf8_1.2.1        stringi_1.6.2     Rcpp_1.0.7        vctrs_0.3.8      
[69] dbplyr_2.1.1      tidyselect_1.1.1  xfun_0.24
igordot commented 3 years ago

The ggsave option does not work anymore for me either. This may be an R 4.1 issue, but I have not investigated. In the meantime, you can try an alternate approach:

pdf("survplot.pdf")
print(surv_plot, newpage = FALSE)
dev.off()
sstandage commented 3 years ago

I have encountered the same problem with ggsave in R 4.1. The alternate approach proposed by igordot worked for me. Thanks!

northNomad commented 3 years ago

The ggsave option does not work anymore for me either. This may be an R 4.1 issue, but I have not investigated. In the meantime, you can try an alternate approach:

pdf("survplot.pdf")
print(surv_plot, newpage = FALSE)
dev.off()

This works well, thanks.

sciencepeak commented 3 years ago

I encounter the same issue that the ggsave doesn't work with ggsurvplot object any more. ggsave() can designate the width and length in mm, which is convenient for different requirements from different journals. So my workaround is this:

pdf(file = "survplot.pdf",
    width = measurements::conv_unit(x = 183, from = "mm", to = "inch"),
    height = measurements::conv_unit(x = 183, from = "mm", to = "inch"))

print(a_ggsurvplot_object, newpage = FALSE)

dev.off()

Anyway, I hope the developers can fix this bug with ggsave()

DanChaltiel commented 3 years ago

It seems that there is a plot member in the ggsurvplot object that we can use.

Therefore, this works for me: ggsave("plot.png", print(a_ggsurvplot_object$plot), width=17, height=10, units="cm")

Of course, this is only a workaround, as this will probably not take the table into account.

fei0810 commented 3 years ago

You can try saving plot member and table member separately, and then remerging by cowplot package or patchwork package

p <- ggsurvplot(fit, data = lung)
p1 = p$plot
p2 = p$table
plotp = cowplot::plot_grid(p1,p2,align = "v",ncol =1,rel_heights = c(4,1))
ggsave(filename = "test.pdf", plot = plotp, device = "pdf")
NikNakk commented 2 years ago

Another workaround is to use ggsave("plot.pdf", survminer:::.buildggsurvplot(my_plot)). I think it's worth considering making survminer:::.buildggsurvplot(my_plot) an exported function since it is useful at times to be able to get the resultant gtable for downstream manipulation. I've therefore created issue #569 for this.

jacobjcs commented 8 months ago

Thanks, it works for me.

zpeng2020 commented 1 month ago

You can try saving plot member and table member separately, and then remerging by cowplot package or patchwork package

p <- ggsurvplot(fit, data = lung)
p1 = p$plot
p2 = p$table
plotp = cowplot::plot_grid(p1,p2,align = "v",ncol =1,rel_heights = c(4,1))
ggsave(filename = "test.pdf", plot = plotp, device = "pdf")

This might be the best solution so far!