ggsave Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "c('ggsurvplot', 'ggsurv', 'list')"
Asked Answered
P

1

6

So I made a ggplot using a survifit curve.

Wolcurves <- survfit(Surv(Days, Survival) ~ Wol, data = DCV_noPBS)
IFB_plot <- ggsurvplot(Wolcurves, data = DCV_noPBS,
                       line = c(2, 1),
                       pval = "p-value = 0", 
                       xlab = "Time in days",
                       ylab = "Proportional survival",
                       break.time.by = 1,
                       break.y.by = 0.2,
                       legend.labs = c("w1118wMel-Tet", "w1118wMel"))

I came up against this error with the following code.

ggsave(filename = "results/IFB2.pdf", plot = IFB_plot,
       width = 12, height = 10, dpi = 1000, units = "cm")

Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "c('ggsurvplot', 'ggsurv', 'list')"

It works fine if I don't specify the plot argument and just save default most recent plot.

Placate answered 18/1, 2022 at 6:47 Comment(0)
F
3

Looks like the survminer package makes a grob object that works with ggsave but then it is turned into a custom list object that doesn't work. I used their internal functions to make a workaround! https://github.com/kassambara/survminer/blob/54f0564a3bdc13441d0b6859bf0a1a6753b45684/R/ggsurvplot.R

Below is a solution that should work in your case! Maybe someone who works on the package has a better solution to add a save option.

Wolcurves <- survfit(Surv(Days, Survival) ~ Wol, data = DCV_noPBS)
IFB_plot <- ggsurvplot(Wolcurves, data = DCV_noPBS, line = c(2, 1), pval = "p-value = 0",
                       xlab = "Time in days", ylab = "Proportional survival",
                       break.time.by = 1, break.y.by = 0.2,
                       legend.labs = c("w1118wMel-Tet", "w1118wMel"))

ggsave_workaround <- function(g){survminer:::.build_ggsurvplot(x = g,
                                                               surv.plot.height = NULL,
                                                               risk.table.height = NULL,
                                                               ncensor.plot.height = NULL)}

g_to_save <- ggsave_workaround(IFB_plot)

ggsave(filename = "results/IFB2.pdf", plot = g_to_save,
       width = 12, height = 10, dpi = 1000, units = "cm")
Firedrake answered 20/3, 2023 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.