survminer icon indicating copy to clipboard operation
survminer copied to clipboard

Error: Cannot convert object of class ggsurvplotggsurvlist into a grob ,when i want to ggarrange a box plot on the left and ggsurvplot on the right

Open ZihaoXingUP opened this issue 3 years ago • 7 comments

ggarrange(diff.exp.plot, ggsurvline.plot) Error: Cannot convert object of class ggsurvplotggsurvlist into a grob ,when i want to ggarrange a box plot on the left and ggsurvplot on the right

ZihaoXingUP avatar Mar 03 '21 07:03 ZihaoXingUP

Experiencing a similar issue trying to use par(mfrow=c(1,2))

library(survival)
library(survminer)

# example data frame
xx = data.frame(stim = c(2, 1:4),
                sts = rep(1, 5))

# survfit
oo = survfit(Surv(time = stim, event = sts) ~ 1, data = xx)

par(mfrow=c(1,2))

# plot 1
plot(oo)

# ggsurvplot
ggsurvplot(fit = oo, xlim=c(-2, 5))

The ggsurvplot is produced as an separate figure without the original plot on the side.

jalavery avatar Mar 25 '21 14:03 jalavery

Experiencing a similar issue trying to use par(mfrow=c(1,2))

library(survival)
library(survminer)

# example data frame
xx = data.frame(stim = c(2, 1:4),
                sts = rep(1, 5))

# survfit
oo = survfit(Surv(time = stim, event = sts) ~ 1, data = xx)

par(mfrow=c(1,2))

# plot 1
plot(oo)

# ggsurvplot
ggsurvplot(fit = oo, xlim=c(-2, 5))

The ggsurvplot is produced as an separate figure without the original plot on the side.

i try your codes ,but it din't work

library(survival)
library(survminer)

# example data frame
xx = data.frame(stim = c(2, 1:4),
                sts = rep(1, 5))

# survfit
oo = survfit(Surv(time = stim, event = sts) ~ 1, data = xx)

par(mfrow=c(1,2))

# plot 1
plot(oo)

# ggsurvplot
ggsurvplot(fit = oo, xlim=c(-2, 5))

thanks your advice. but ggsurvplot based on ggplot , not on the par plot system. so ,it can't draw multi plots on a picture when i try your codes. image

if i don't use ggsurvplot, i do can draw multi plots on one figure.

library(survival)
library(survminer)

# example data frame
xx = data.frame(stim = c(2, 1:4),
                sts = rep(1, 5))

par(mfrow=c(1,2))
oo = survfit(Surv(time = stim, event = sts) ~ 1, data = xx)

# plot a simple boxplot
plot(1:5,2:10)

#plot a survline plot
plot(oo)

image

ZihaoXingUP avatar Mar 29 '21 08:03 ZihaoXingUP

This is the solution: plot1<–datos %>% ggsurvplot(fit1,data=.) plot1<–datos %>% ggsurvplot(fit2,data=.)

ggarrange(plotlist=list(plot1$plot,plot2$plot), labels = c("A", "B",), ncol = 1, nrow = 2)

fufo64 avatar Jun 05 '21 16:06 fufo64

This is the solution: plot1<–datos %>% ggsurvplot(fit1,data=.) plot1<–datos %>% ggsurvplot(fit2,data=.)

ggarrange(plotlist=list(plot1$plot,plot2$plot), labels = c("A", "B",), ncol = 1, nrow = 2)

Could you explain the code “plot1<–datos %>% ”? This does not work in my computer. Thank you.

jijitoutou avatar Aug 30 '21 08:08 jijitoutou

This is the solution: plot1<–datos %>% ggsurvplot(fit1,data=.) plot1<–datos %>% ggsurvplot(fit2,data=.) ggarrange(plotlist=list(plot1$plot,plot2$plot), labels = c("A", "B",), ncol = 1, nrow = 2)

Could you explain the code “plot1<–datos %>% ”? This does not work in my computer. Thank you.

plot1<–datos %>% ggsurvplot(fit1,data=.) is equivalent to: plot1<– ggsurvplot(fit1, data=datos) where datos is a data frame with survival information. Does that help to clarify?

jalavery avatar Sep 08 '21 16:09 jalavery

helps clarifying but it still doesn't solve the issue in my case.

Many thanks,

Jenny

JennyCNS avatar Jun 05 '23 14:06 JennyCNS

Late to the answer here, but the output from ggsurv contains the combine output including plot and data, which is why it throws an error with ggarrange.

The answer by @fufo64 is correct in that specifying the $plot from the ggsurvplot output in ggarrange works, but there's no need to add them as a list or include the pipe in the ggsurv call.

Reproducible example here:

require("survival")
require(ggpubr)

fit<- survfit(Surv(time, status) ~ sex, data = lung)

a <- ggsurvplot(fit, data = lung)
b <- ggsurvplot(fit, data = lung, surv.median.line = "hv", palette = c("#E7B800", "#2E9FDF"))

ggarrange(a$plot,b$plot,ncol=2, widths=c(1,1), labels = c("a", "b"))

marine-ecologist avatar Jul 04 '23 21:07 marine-ecologist