如何使用 knitr 和 RMarkdown 在一个块中的多个页面中添加多个图形?

How to add multiple figures across multiple pages in a chunk using knitr and RMarkdown?

我正在使用 for 循环通过 knitr 和 rmarkdown 在一个块中跨多个页面创建多个大数字。它适用于 word 和 html 输出,但在 pdf 输出中有一个问题。

这是重现我的问题的最小 RMarkdown 示例。

---
title: "Knitr test"
date: "6 April 2015"
output: pdf_document
---


```{r, echo=FALSE, fig.width=6.5,fig.height=10}
library(ggplot2)
for (i in seq(1, 4)){
    p <- ggplot(cars, aes(speed, dist)) + geom_point() 
    print(p)
}
```

生成的 pdf 文件如下所示。页面中打印了两个数字。

如果我更改 fig.height,在 rmd 文件中添加几个部分,两个图仍然打印在同一页上,但排列不同。

---
title: "Knitr test"
output: pdf_document
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=8, fig.width=6.5}
library(ggplot2)
for (i in seq(1, 4))
{
    p <- ggplot(cars, aes(speed, dist)) + geom_point()
    print(p)
}

```

感谢您提出解决此问题的任何建议。

我正在使用 RStudio 0.99.375。这是我的会话信息。

sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmarkdown_0.5.3.1 knitr_1.9.5      

loaded via a namespace (and not attached):
 [1] colorspace_1.2-5 digest_0.6.8     evaluate_0.5.5   formatR_1.0     
 [5] ggplot2_1.0.0    grid_3.1.3       gtable_0.1.2     htmltools_0.2.6 
 [9] MASS_7.3-34      munsell_0.4.2    plyr_1.8.1       proto_0.3-10    
[13] Rcpp_0.11.5      reshape2_1.4.1   scales_0.2.4     stringr_0.6.2   
[17] tcltk_3.1.3      tools_3.1.3      yaml_2.1.13     

编辑 1 (LaTeX):我似乎无法让它与 RMarkdown 一起使用,因为它对新页面有问题。但是使用纯 LaTeX 似乎解决了单页中的多个图和开头的文本的问题。不确定它是否是你想要的。在 RStudio 中打开一个新的 R Sweave (.Rnw) 文件并尝试:

\documentclass{article}
\begin{document}
\title{A report}
\author{Me}
\maketitle
\section{One section}
Some text that does not say anything interesting.

<<r, echo=FALSE, fig.width=6.5, fig.height=7>>=
library(ggplot2)
for (i in seq(1, 4)){
  p <- ggplot(cars, aes(speed, dist)) + geom_point()
  print(p)
}
@

\end{document}

这会产生以下结果:

编辑 2 (RMarkdown)(根据你的回答):运行你回答的代码我得到第一页的文字,然后是一个每个情节的页面。如果这是你想要的,你可以简单地使用(即删除 grid.newpage\rs 和 keep_tex)获得相同的结果:

---
title: "Knitr test"
output: pdf_document
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=10, fig.width=6.5}
library(ggplot2)
for (i in seq(1, 4))
{
    p <- ggplot(cars, aes(speed, dist)) + geom_point()

    print(p)
    cat('\n\n')
}


```

产生以下结果:

如果将 fig.height 更改为 7,您将获得与上述 LaTeX 相同的结果。

我的问题已经解决了。

在生成的tex文件中,每个图后面没有换行。我们使用上面的 rmd 文件生成的这个 tex 代码:

\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}

解决办法是每次循环后加一行打印一个图形。

cat('\r\n\r\n')

不知道为什么我在这里需要两个“\r\n”。生成的 tex 文件如下所示:

\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}

这是我的 Rmd 文件的完整示例

---
title: "Knitr test"
output:
  pdf_document:
    keep_tex: yes
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=8, fig.width=6.5}
library(ggplot2)
library(grid)
for (i in seq(1, 4))
{
    grid.newpage()
    p <- ggplot(cars, aes(speed, dist)) + geom_point()

    print(p)
    cat('\r\n\r\n')
}


```

在你的 markdown 解决方案中,我无法执行包含三个垂直堆叠的数字减少 fig.height 参数。似乎## Section A 保持独立

## Frecuencias
```{r frecuencia, fig.cap = 'Frecuencias', fig.subcap=c('Total', 'Judicial', 'Gravedad e incapacidad'), fig.ncol=1, fig.align='center', out.height='25%'}
vals$plotfrectotal
vals$plotfrecjud
vals$plotfrecporinc
```