Search code examples
rpdfggplot2r-markdownline-breaks

Line break between table and plot with pdf_document output in R Markdown


I'm creating a document with PDF output in R Markdown.

My code creates a number of tables and figures in a "for" loop, within the same code chunk. All the tricks I've seen to create a line break in a PDF document - with all sorts of combinations and permutations of preceding slashes and spaces (e.g., newline, linebreak,
, &nbsp, etc) are not working. So, I decided to cut some corners and just increase the plot space in my ggplot2 margins to create the illusion of more white space between my table and my graph.

  • Ideally, I'd love to know how to actually insert white space ( a line, or several lines) properly within a single code chunk, without getting an error.
  • However, I'd also settle for figuring out why the following code "works" to produce extra space in my ggplot2 plot when I have some background color (first plot) but doesn't work when the background color is white!!? This is truly bizarre behavior folks!

See my screenshot below and the code, with two identical code chunks - except the background color of the plot - where one plot with a light beige background shows the desired "white space" between the table and the plot title, and the plot following (white background) does not show the same "white space".

R markdown pdf output - first plot has yellow background with linebreak, second plot is white background without linebreak

---
title: "I need a line break"
output: pdf_document
---

```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE, 
                  include = TRUE, 
                  message = FALSE, 
                  warning = FALSE, 
                  error = FALSE)
```

```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)

print(mytable)

myplot <- ggplot(iris) + 
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void() +
theme(plot.background = element_rect(fill = "#FFFEEB", color = "white"),
  plot.margin = unit(c(1, 0, 0, 0), "cm"))
print(myplot)
}
```


```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)

print(mytable)

myplot <- ggplot(iris) + 
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
  theme_void() +
  theme(plot.background = element_rect(fill = "white", color = "white"),
  plot.margin = unit(c(1, 0, 0, 0), "cm"))

print(myplot)
}
```

Solution

  • Can you add linebreaks with cat to solve your problem? E.g.

    ---
    title: "I need a line break"
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    library(ggplot2)
    library(knitr)
    library(ggplot2)
    knitr::opts_chunk$set(echo = FALSE, 
                      include = TRUE, 
                      message = FALSE, 
                      warning = FALSE, 
                      error = FALSE)
    ```
    
    ```{r, results = "asis", fig.height = 2}
    for (i in 1:2) {
    mytable <- kable((iris[c(1,i),]), booktabs = TRUE)
    print(mytable)
    cat("\\ ")
    cat("\\linebreak")
    cat("\\linebreak")
    cat("\\linebreak")
    myplot <- ggplot(iris) + 
    labs(title = "Gorgeous plot") +
    geom_point(aes(Sepal.Length, Sepal.Width)) +
      theme_void()
    print(myplot)
    cat("\\linebreak")
    cat("\\linebreak")
    }
    ```
    

    example_1.png

    (The cat("\\ ") is needed so that there is a line to end)