13
votes

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.

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

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

enter image description here

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

```

enter image description here

Thanks for any suggestion to fix this problem.

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     
3

3 Answers

14
votes

I have solved my problem.

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

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}

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


```
7
votes

Edit 1 (LaTeX): I can't seem to get this to work with RMarkdown, since it has issues with new pages. But using pure LaTeX seems to solve the issue with the multiple plots in a single page and the text at the beginning. Not sure though if it's what you want. In RStudio open a new R Sweave (.Rnw) file and try:

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

This produces the following:

just a pdf

Edit 2 (RMarkdown) (based on your answer): running the code from your answer I get the first page with the text, and then a single page for each plot. If this is what you want, you can get the same result simply using (i.e. removing grid.newpage, \rs and 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')
}


```

Which produces the following:

other screenshot

If you change the fig.height to 7, you'll get the same result as with LaTeX above.

0
votes

in your markdown solution, I cannot perform the inclusion of three figures stacked vertically reducing the fig.height parameter. It seems the ## Section A remains alone

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

result