如何使用knitr和RMarkdown在块中的多个页面上添加多个图形? [英] How to add multiple figures across multiple pages in a chunk using knitr and RMarkdown?

查看:85
本文介绍了如何使用knitr和RMarkdown在块中的多个页面上添加多个图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用for循环使用knitr和rmarkdown在一个块中跨多个页面创建多个大图形.它适用于word和html输出,但是在pdf输出中存在一个问题.

I am using a for loop to create multiple big figures across multiple pages in one chunk with knitr and rmarkdown. It works fine for word and html outputs, but has one problem in pdf output.

这是一个重现我的问题的最小RMarkdown示例.

This is a minimal RMarkdown example to reproduce my problem.

---
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文件如下所示.页面上打印了两个数字.

The generated pdf file looks like this. Two figures are printed in the page.

如果更改fig.height,请在rmd文件中添加几节内容,但仍然在同一页面上以不同的顺序打印了两个图形.

If I change the fig.height, add a few section in the rmd file, two figures are still printed in the same page with different arrangement.

---
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)
}

```

感谢您提出解决此问题的建议.

Thanks for any suggestion to fix this problem.

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

I am using RStudio 0.99.375. This is my session information.

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     

推荐答案

我已经解决了我的问题.

I have solved my problem.

在生成的tex文件中,每个图形之后没有新行.我们使用上面的rmd文件生成了这个tex代码:

In the generated tex file, there aren't new lines after each figure. This tex code us generated using rmd file above:

\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}

解决方案是在每个循环之后添加新行以打印图形.

The solution is to add a new line after each cycle to print a figure.

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

不确定在这里为什么需要两个"\ r \ n".生成的tex文件如下所示:

Not sure why I need two "\r\n" here. The generated tex file looks like:

\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文件的完整示例

This is the full example of my Rmd file

---
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')
}


```

这篇关于如何使用knitr和RMarkdown在块中的多个页面上添加多个图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆