多个ggplots在knitr中打印到单个页面

问题描述 投票:0回答:1

这个问题的轻微变化已经出现了很多,但似乎没有一个对我的情节有所帮助。

我在R中使用survminer::ggsurvplot作为生存图,我想将编织器代码编译为pdf。我有6个图,所以我想在一个页面上以2列显示它们。我的代码看起来像这样(减去2个图来节省空间):

<<echo=FALSE, fig=TRUE,out.width = ".45\\textwidth", fig.ncol=2>>=

# Overall Survival for each marker
fit.DFS.AR <- survfit(Surv(TNBC1$DF.time, TNBC1$DFS)  ~ TNBC1$AR.bin,
           data = TNBC1)
fit.DFS.CK <- survfit(Surv(TNBC1$DF.time, TNBC1$DFS)  ~ TNBC1$CK.bin,
           data = TNBC1)
fit.DFS.Cla <- survfit(Surv(TNBC1$DF.time, TNBC1$DFS)  ~ TNBC1$Cla.bin,
           data = TNBC1)
...
# Visualize with survminer
plot.arDF <- print(ggsurvplot(fit.DFS.AR, data = TNBC1 , risk.table = F, xlim = c(0, 60),
       #surv.median.line = "hv",
       xlab="Time (months)", ylab="Overall Survival Probability") +   ggtitle("AR") )
plot.ckDF <- print(ggsurvplot(fit.DFS.CK, data = TNBC1 , risk.table = F, xlim = c(0, 60),
       #surv.median.line = "hv",
       xlab="Time (months)", ylab=" Overall Survival Probability") + ggtitle("CK") )
plot.claDF <- print(ggsurvplot(fit.DFS.Cla, data = TNBC1 , risk.table = F, xlim = c(0, 60),
       #surv.median.line = "hv",
       xlab="Time (months)", ylab=" Overall Survival Probability") + ggtitle("Claudin1") )
...
gridExtra::grid.arrange(plot.arDF$plot + theme(plot.title = element_text(hjust = 0.5)),
    plot.ckDF$plot + theme(plot.title = element_text(hjust = 0.5)),
    plot.claDF$plot + theme(plot.title = element_text(hjust = 0.5)),                     
    ncol=2, nrow=3)

结果是一列图表在页面上运行。有没有办法在不切换到rmd的情况下将5个图拼接在一个页面上?我也试过没有情节标签和没有grid.arrange的情节。

r ggplot2 knitr survival-analysis
1个回答
1
投票

survminer显然有自己的排列功能arrange_ggsurvplots,可用于在同一页面上安排多个ggsurvplots。

# Fit survival curves
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# List of ggsurvplots
require("survminer")
splots <- list()
splots[[1]] <- ggsurvplot(fit, data = lung, risk.table = TRUE,
                          ggtheme = theme_minimal())
splots[[2]] <- ggsurvplot(fit, data = lung, risk.table = TRUE,
                          ggtheme = theme_grey())

# Arrange multiple ggsurvplots and print the output
arrange_ggsurvplots(splots, print = TRUE,
                    ncol = 2, nrow = 1, risk.table.height = 0.4)

## Not run: ------------------------------------
# # Arrange and save into pdf file
# res <- arrange_ggsurvplots(splots, print = FALSE)
# ggsave("myfile.pdf", res)
## ---------------------------------------------

enter image description here

详细信息请访问:http://www.sthda.com/english/rpkgs/survminer/reference/arrange_ggsurvplots.html

© www.soinside.com 2019 - 2024. All rights reserved.