使用一个共享图例排列 ggsurv 图

Arrange ggsurv plots with one shared legend

我正在尝试在 R 中使用共享图例在网格中绘制多条 Kaplan Meijer 曲线。建议在此处 (common legend in arrange_ggsurvplots) ggsurvplot_facet。然而,在我的例子中,每个图都是基于使用不同变量的不同拟合,所以 ggsurvplot_facet (至少在我的理解中)在这里不适用。请在下面查看我的数据和脚本。有没有办法在共享一个传说的同时安排这些情节?我试过使用 ggarrangegrid_arrange_shared_legend,但无法使用 ggsurvplot 对象。

library(survival)
library(survminer)

data_km <- data.frame(DaysToFirstDetectByBird = c(16.88, 50.17, 1.14, 22.46, 9.95, 4.64, 1.08, 7.06, 1.86, 0.00, 1.11, 2.87, 3.63, 29.15, 0.31, 13.89, 2.16, 2.24, 5.93, 0.12, 0.92, 0.06, 0.08, 0.32, 1.23, 19.06, 8.09, 0.17, 16.04, 4.86, 4.11, 2.94, 0.06, 5.69, 4.87),
           FirstDetectByBirdEvent = c(1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1),
           DaysToFirstDetectByMammal = c(1.63, 6.47, 1.44, 1.62, 1.22, 1.13, 2.22, 3.43, 10.66, 2.37, 20.83, 0.64, 1.09, 1.46, 0.49, 0.72, 0.90, 0.59, 6.05, 10.43, 3.04, 0.68, 0.53, 27.47, 0.93, 2.57, 0.46, 0.68, 2.61, 5.32, 0.69, 0.22, 0.42, 0.51, 0.50),
           FirstDetectByMammalEvent = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
           OcGroup = c("Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest", "Forest"))

# plotA
SurvFit_DetectionBird <- survfit(Surv(DaysToFirstDetectByBird, FirstDetectByBirdEvent) ~ OcGroup, data = data_km)
plot_DetectionBird <- ggsurvplot(SurvFit_DetectionBird)
plot_DetectionBird$plot <- plot_DetectionBird$plot + labs(title = "A")

# plotB
SurvFit_DetectionMammal <- survfit(Surv(DaysToFirstDetectByMammal, FirstDetectByMammalEvent) ~ OcGroup, data = data_km)
plot_DetectionMammal <- ggsurvplot(SurvFit_DetectionMammal)
plot_DetectionMammal$plot <- plot_DetectionMammal$plot + labs(title = "B")

# arrange plots
plots <- list(plot_DetectionBird, plot_DetectionMammal)
res <- arrange_ggsurvplots(plots, print = FALSE,
                           ncol = 1, nrow = 2)

ggsave("~/Desktop/survplots_test.pdf", res)

你可以使用patchwork包,但首先你需要从"ggsurvplot"对象中提取ggplot对象

library(patchwork)
(plot_DetectionBird$plot + plot_DetectionMammal$plot) / guide_area() + 
  plot_layout( guides = 'collect')