kassambara / survminer

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

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 #517

Open ZihaoXingUP opened 3 years ago

ZihaoXingUP commented 3 years ago

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

jalavery commented 3 years ago

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.

ZihaoXingUP commented 3 years ago

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

fufo64 commented 3 years ago

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)

jijitoutou commented 3 years ago

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.

jalavery commented 3 years ago

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?

JennyCNS commented 1 year ago

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

Many thanks,

Jenny

marine-ecologist commented 1 year ago

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"))
hsiaoyi0504 commented 1 week ago

If you are using cowplot, plot_grid(a$plot,b$plot) also works