3

I struggle with a blank page problem in my knited PDF R markdown document. It appears that when I plot 2 ggplot() graphs in a single code chunk, a blank page is created before rendering the graphs. Here is an example of the problem (complete .Rmd file, uncomment beginning and ending of each chunk to run, where UNCOMMENT stands!):

---
title: "Test"
author: "My Name"
output:
  pdf_document:
    number_sections: yes
    toc: yes
---

\newpage

# Plots

## First subtitle

Here I plot some data:

#UNCOMMENT```{r pressure, echo=FALSE, message = FALSE}
library(tidyverse)

# line chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line()

# step chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_step()

#UNCOMMENT```

\newpage

# More plots

#UNCOMMENT```{r, echo = FALSE}

# line chart with points
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line() +
        geom_point()

# line chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line()

#UNCOMMENT```


When knited, the PDF output has a blank page before my "More plots" chunk, i.e. 4th page is blank:

[![pdf output][1]][1] PDF output

Any idea why it happens and how to solve that issue? If I separate the 2 plots in 2 different chunks the problem is solved but I want to plot multiple graphs in some chunks so it's not a solution.

Thanks in advance!

ludo
  • 169
  • 1
  • 12

2 Answers2

2

Giving credit to this link. using the captions helps center the plots. This is just a suggestion not an explicit answer to your question.

---
 title: "Test"
 author: "My Name"
 output: pdf_document
 header-includes:
   - \usepackage{subfig}
---  

```{r echo =FALSE, results=FALSE}
captions <- c("Caption 1",
              "Caption 2", 
              "Caption 3",
              "Caption 4")
```

```{r, echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE,  comment=FALSE, message= FALSE, eval =T,  fig.cap =    "Overall Caption", fig.subcap=captions, out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
library(ggplot2)
 # line chart
 p1<- ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line()

 # step chart
 p2<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_step()


 # line chart with points
 p3<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line() +
         geom_point()

 # line chart
 p4<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line()


 p1
 p2
 p3
 p4
 ```      
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • Hi, I use R version 4.0.0, rmarkdown_2.1 on x86_64-apple-darwin17.0 (64-bit) macOS Catalina 10.15.5. A large output view doesn't change the problem... strange. – ludo Jun 08 '20 at 00:10
  • R prints the pdf and displays it in a third party browser, and then I print from there, can you change what PDF program R opens up to view the PDF? – Daniel_j_iii Jun 08 '20 at 00:28
  • Yes I tried to view the file with other programms but I still have this blank page.. – ludo Jun 08 '20 at 22:19
  • I changed my answer, please see above – Daniel_j_iii Jun 08 '20 at 23:05
  • Thanks, indeed this is useful. I can use it as a good workaround and I'll use it for other purpose too. – ludo Jun 09 '20 at 11:44
1

I believe this behavior stems from the fact that LaTeX deals with figures as floats. You can try updating your YAML to read

...
output: 
    pdf_document:
        # improve float placement
        # see https://bookdown.org/yihui/rmarkdown-cookbook/figure-placement.html
        extra_dependencies: ["flafter"]

or

...
  output: 
    pdf_document:
        # Another way to try and control float placement
        extra_dependencies: ["float"]

Note that spacing matters in how the YAML is interpreted by knitr.

Another option would be to make the figures a little bit smaller and see if that helps with float placement.

mikemtnbikes
  • 393
  • 2
  • 11
  • Thanks for this. I had a script that was previously working (figures with no blank pages between) and suddenly it was no longer working. I tried your first suggestion (`flafter`) but that didn't work. I tried shrinking the plots (by 0.1 inch!) and that did work. – Nova Jul 18 '23 at 17:14