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.
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.
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 code, followed by two extra spaces (e.g.  .. , replacing the periods with spaces).
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 .
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 $$ ... $$
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With