Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用knitr和RMarkdown在块中的多个页面上添加多个图形?_R_Rstudio_Knitr_R Markdown - Fatal编程技术网

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

如何使用knitr和RMarkdown在块中的多个页面上添加多个图形?,r,rstudio,knitr,r-markdown,R,Rstudio,Knitr,R Markdown,我使用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

我使用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)
}
```
编辑1(LaTeX):我似乎无法将其与RMarkdown一起使用,因为它在新页面中存在问题。但是使用纯乳胶似乎可以解决在一页中有多个情节,并且在开头有文本的问题。不知道这是否是你想要的。在RStudio中打开一个新的R swave(.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}
这将产生以下结果:


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

我已经解决了我的问题

在生成的tex文件中,每个图形后面没有新行。此tex代码使用上面的rmd文件生成:

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


```
---
标题:“Knitr测试”
输出:
pdf\U文件:
泰克斯:是的
日期:“2015年4月6日”
---
##A节
B排
```{r plot_表型,echo=FALSE,fig.height=8,fig.width=6.5}
图书馆(GG2)
图书馆(网格)
第(1,4)项中的(i)项
{
grid.newpage()

p在您的降价解决方案中,我无法通过减少fig.height参数来执行包含三个垂直堆叠的图形的操作。看起来##部分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
```

谢谢你的建议。如果高度更改为8,则不需要cat。是的,玩高度可以工作,但不确定为什么会导致其他高度出现此问题。@Bangyu是的,对不起,我玩的是
cat
,同时玩的是高度,所以我没有意识到高度是主要问题。我认为没有简单的方法o获得完美的pdf(使用RMarkdown)还有一个自动的for循环,与这样的文本相结合。它似乎总是在同一页中组合两个绘图。我的答案与包含的部分等不匹配。@bangyu我用LaTeX更改了我的答案,效果很好。整个RMarkdown到Markdown到pdf可能会导致图形排列出现问题。谢谢你你的建议。LaTex对我有效。但我更喜欢直接使用RMarkdown。我稍后会接受你的答案,看看是否还有其他答案可以使用RMarkdown。如果你正在使用的绘图是一个带有多个绘图栏的GTTable,你可以用
grid.draw(p)替换
print(p)
,它仍将工作。如果不工作,它还将呈现TableGrob输出。
\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}
---
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')
}


```
## 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
```