3

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

1 Answer 1

3
+50

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did work. I had to add an extra cat("\\ ") after the cat("\\linebreak") as well, but yes - that did the trick. I have to admit, I'm still curious about the weird spacing issue in the plot, but I can always ask another question or post an issue on github about it. I'll flip you the bounty asap!
Thanks @Nova, I'm not sure about your weird spacing issue though - I wasn't able to reproduce the problem on my system (macOS, R v4.0.3, RStudio v1.3.1093) so you would need to provide more details e.g. the output from sessionInfo() and versions of pandoc / cairo / whatever you're using to render the .Rmd. I suspect it has to do with voiding the theme (i.e. theme_void() cf. theme_classic()) but without being able to reproduce the issue I couldn't come up with a solution sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.