Open kassambara opened 7 years ago
ggsurvplot()
returns a list of ggplots containing survival curves and optionally the risk table. You can’t directly save the list using ggsave(), but you can save the output of print(ggsurvplot)
.
First, please install the latest version of survminer:
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/survminer", build_vignettes = FALSE)
Then, type this:
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
require("survminer")
survp <- ggsurvplot(fit, risk.table = TRUE)
ggsave(file = "ggsurv.pdf", print(survp))
It should work.
Hi, How can I use ggsave if I dont need the "number at risk table" ?
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
require("survminer")
survp <- ggsurvplot(fit, risk.table = FALSE)
ggsave(file = "ggsurv.pdf", print(survp))
Saving 4.46 x 7 in image Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "list"
Hi,
To save ggsurvplot + risktable, use this:
pdf("survplot.pdf")
print(survp, newpage = FALSE)
dev.off()
To save only survival plots without the risk table, use this:
pdf("survplot.pdf")
print(survp$plot, newpage = FALSE)
dev.off()
or use this:
ggsave("survplot.pdf", surp$plot)
Im getting a similar error. Please see my code below.
fit<- survfit(Surv(time, dead) ~ copy_number, data = datain) p <- ggsurvplot(fit, conf.int = TRUE, pval = TRUE, main = "Survival", palette = c("#ff0000", "#0000ff"), font.main = c(10, "bold"), font.x = c(8, "bold"), font.y = c(8, "bold"), font.tickslab = c(6, "bold"), pval.size = 2, size = 0.2, censor.size = 1, legend = "bottom" ) p$plot <- p$plot + theme(legend.text = element_text(size = 4), legend.title = element_text("", size = 4), legend.key.size = unit(0.2, "cm"))
ggsave(filename= "/home/philserver/Desktop/mt_amp.tiff", print(p), device = "tiff", height = 2, width = 2, dpi = 300)
Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "list"
In spite of this error, the plot is still saved. However, the error stops the R pipeline I have written, and the other dozen graphs in my pipeline are not generated. Is there a way to resolve the issue of saving ggsurvplots without throwing an error?
You can't use, the standard ggsave(), because ggsurvplot() always generates an object of class list, even if you set risk.table to FALSE. I think that I already suggested a solution to this issue. Please let me know if the solution works for you.
The function ggpubr::ggexport() should work also for a list of ggplots, which are not supported by ggsave
To anyone reading this, there is still no ggsave method for ggsurvplot objects.
no, let's open this issue
ggsave(
filename ='os-plot.pdf',
plot = print(os.plot, newpage = FALSE),
device = 'pdf',
path = 'data/output',
width = 8,
height = 9
)
Worked for me.
This still does not work in version 0.4.9 e.g.:
ggsave(filename = "./debug/test_picture.png",plot = print(obj), device = "png")
produces a blank image for a simple Kaplan-Meier plot with number at risk table. generated with survminer.
Similarly the producing a plot to pdf method produces an unreadable pdf (error on loading).
UPDATE: the ggexport method does work, producing one blank image and one plot. The full plot is produced with the name (using the above example) test_picture002.png (001 is blank).
ggexport(filename = "./debug/test_picture.png",plot = obj, device = "png")
Hopefully useful for someone!
Neither the ggsave nor the ggexport works well for me.
I'm getting blank output with ggsave whether I try to save as png or pdf. ggexport will allow me to save as a png, but for publication I need it in pdf. To get a pdf, I have to export using the RStudio utility and some elements are not rendered accurately. Any more ideas/changes?
There's a fix already, but you can use the code before it's been updated in the CRAN
grid.draw.ggsurvplot <- function(x){
survminer:::print.ggsurvplot(x, newpage = FALSE)
}
Run this function before you save your plot using ggsave
to save your plot.
The code below should save the survival plot with risk table.
library("survival")
library("survminer")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
p <- ggsurvplot(fit, risk.table = TRUE)
# add method to grid.draw
grid.draw.ggsurvplot <- function(x){
survminer:::print.ggsurvplot(x, newpage = FALSE)
}
# Remember to pass object `p`.
ggsave("survival.png", plot = p,
dpi=300, width = 10, height = 7, units = "in")
@mleipzig It works perfectly fine, I tried the all the code with the first code. You forgot to pass the plot object. The
ggsurvplot
function returns aggsurvplot
object, not aggplot
object and it will not write anything tolast_plot()
. Readggsave
function help thoroughly. I think you should correct your plot saving code:ggsave("one_year_survival.tiff", plot = p, device="tiff", dpi=300, width = 20, height = 7, units = "in")
None of these work for me, the best I get is the last plot being saved , i.e. the KM plot if there is no risk table, or the risk table alone.
This works for me: ggsave(file="survival.pdf", plot=ggarrange(g$plot, g$table, nrow=2, ncol=1, heights=c(3,1)))
But this would have to be manually adapted based on the number of subplots. Also, the x-axes become misaligned.
This function is a workaround
save_ggsurvplot <- function(filename, g, rel_heights=c(3,1,1,1)){ nrplots=length(g)-2 ggsave(file=filename, plot=ggarrange(plotlist=g[1:nrplots], nrow=nrplots, ncol=1, align="v", heights=rel_heights[1:nrplots])) }
Maybe it could be possible to convert the res
object evaluated in ggsurvplot::print.ggsurvplot
to a proper ggplot object and return it.
In the meantime, the following behind-the-scenes workaround, seems working to me:
library("survival")
library("survminer")
#> Loading required package: ggplot2
#> Loading required package: ggpubr
#>
#> Attaching package: 'survminer'
#> The following object is masked from 'package:survival':
#>
#> myeloma
fit <- survfit(Surv(time, status) ~ sex, data = lung)
gg_risk <- ggsurvplot(fit, risk.table = TRUE)
risk_png <- tempfile("risk", fileext = ".png")
risk_png |>
ggsave(
plot = survminer:::.build_ggsurvplot(gg_risk)
)
#> Saving 7 x 5 in image
if (!interactive() && requireNamespace("knitr")) {
knitr::include_graphics(risk_png)
}
gg_norisk <- ggsurvplot(fit, risk.table = FALSE)
norisk_png <- tempfile("norisk", fileext = ".png")
norisk_png |>
ggsave(
plot = survminer:::.build_ggsurvplot(gg_norisk)
)
#> Saving 7 x 5 in image
if (!interactive() && requireNamespace("knitr")) {
knitr::include_graphics(norisk_png)
}
Created on 2022-03-11 by the reprex package (v2.0.1)
Maybe it could be possible to convert the
res
object evaluated inggsurvplot::print.ggsurvplot
to a proper ggplot object and return it.In the meantime, the following behind-the-scenes workaround, seems working to me:
library("survival") library("survminer") #> Loading required package: ggplot2 #> Loading required package: ggpubr #> #> Attaching package: 'survminer' #> The following object is masked from 'package:survival': #> #> myeloma fit <- survfit(Surv(time, status) ~ sex, data = lung)
With risk table
gg_risk <- ggsurvplot(fit, risk.table = TRUE) risk_png <- tempfile("risk", fileext = ".png") risk_png |> ggsave( plot = survminer:::.build_ggsurvplot(gg_risk) ) #> Saving 7 x 5 in image if (!interactive() && requireNamespace("knitr")) { knitr::include_graphics(risk_png) }
Without risk table
gg_norisk <- ggsurvplot(fit, risk.table = FALSE) norisk_png <- tempfile("norisk", fileext = ".png") norisk_png |> ggsave( plot = survminer:::.build_ggsurvplot(gg_norisk) ) #> Saving 7 x 5 in image if (!interactive() && requireNamespace("knitr")) { knitr::include_graphics(norisk_png) }
Created on 2022-03-11 by the reprex package (v2.0.1)
Session info
Work for me, thanks!
Both examples (from @adayim and @CorradoLanera) work for me (with argument risk.table=TRUE
). It would be very nice, if one of these could be added to the exported functions for the next CRAN release.
e-mail from a user
Hi Alboukadel, How are you? I hope you’re doing good. I was trying to save a KM plot using survminer. However, when I try to save the figure with ggsave I got an error message as below. I did some online search but could not really find a solution. Could you please help? Thanks in advance! Serigne