Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding space around figures in RMarkdown

I would like to add space around figures in RMarkdown. I am knitting to PDF and really don't like how close figures (or also equations) are to the text or to the next figure.

---
output: pdf_document
---

```{r pressure, echo=FALSE}
plot(pressure)
```

```{r pressure2, echo=FALSE}
plot(pressure)
```

There is just too little space between the two plots and this gets more fuzzy when using ggplots.

Right now I use the Latex solution

\vspace{10pt}

but it would be nice if I could make a setting globally for the entire document.

like image 261
Georgery Avatar asked Jan 18 '18 09:01

Georgery


People also ask

How do I add extra space in R markdown?

Another easy way to do this is to just use HTML tags. Adding <br> will give a single line break and I've used that when, for whatever reason, using the (two-space indentation) is ignored. Another fix is putting a backslash `\` just in front of the newline, resulting in a blank line in the html.

How do you add space between lines in R markdown?

Blank Lines To add a single extra line after a paragraph, add two extra spaces at the end of the text. To add an extra line of space between paragraphs, add the HTML &nbsp; code, followed by two extra spaces (e.g. &nbsp.. , replacing the periods with spaces).

How do you insert a line break in Rmarkdown PDF?

Add Line Breaks in R Markdown To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .


1 Answers

Concerning the spacing before and after plots you can use a simple knitr hook:

```{r, echo = F}
library(knitr)
if(is_latex_output()) {
  plot_default <- knit_hooks$get("plot")
  knit_hooks$set(plot = function(x, options) { 
    x <- c(plot_default(x, options), "\\vspace{25pt}")
  })
}
```

Here we alter the plot hook in that sense that we just add a spacing of 25pt after each plot output.

Concerning equations you can just add these four length definitions at the beginning of your document:

\setlength{\abovedisplayskip}{25pt}
\setlength{\belowdisplayskip}{25pt}
\setlength{\abovedisplayshortskip}{25pt}
\setlength{\belowdisplayshortskip}{25pt}

The first two alter equations created using the align environment. The latter two those created using $$ ... $$.

like image 154
Martin Schmelzer Avatar answered Oct 20 '22 10:10

Martin Schmelzer