简体   繁体   English

将多个ggplots打印到knitr的单个页面中

[英]Multiple ggplots printed to a single page in knitr

Slight variations of this question have popped up a lot, but none seem to help me with my plots. 这个问题的轻微变化已经冒出很多,但是似乎没有一个对我的情节有帮助。

I am using survminer::ggsurvplot for survival plots in R and I want to compile the knitr code to a pdf. 我正在使用survminer::ggsurvplot作为R中的生存图,我想将knitr代码编译为pdf。 I have 6 plots so I would like to display them in 2 columns on a single page. 我有6个地块,因此我想在一页上的2列中显示它们。 My code looks like this (minus 2 plots to save space): 我的代码如下所示(减去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)

The result is a single column of figures that run off the page. 结果是整个页面上只有一栏数字。 Is there a way to get the 5 plots together on a single page without switching to rmd? 有没有一种方法可以在不切换到rmd的情况下将5个图合并到一个页面上? I also tried plotting without the plot labels and without grid.arrange . 我还尝试了在没有绘图标签和没有grid.arrange情况下进行grid.arrange

survminer apparently has its own arrange function arrange_ggsurvplots which can be used to arrange multiple ggsurvplots on the same page. 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)
## ---------------------------------------------

在此处输入图片说明

Details can be found at: http://www.sthda.com/english/rpkgs/survminer/reference/arrange_ggsurvplots.html . 可以在以下网站找到详细信息: http : //www.sthda.com/english/rpkgs/survminer/reference/arrange_ggsurvplots.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM