1

First

Second

Third

Does anybody know why R markdown generates excessive white space above each plot and how can I fix that ? Is there a knitr option to be included ? Or any chunk option maybe ?

I've provided 3 images at the bottom of this post so that you can see what I mean.

Haven't used any other chunk option than echo, warning and message and about the plot it is a basic ggplot.

Let me know in the comments if I need to provide any code example of my chunks for a better view.

Edit: here's a simple rmarkdown file generating the same excessive white space above ggplots.

---
title: "Untitled"
author: "Razvan Cretu"
date: "January 9, 2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r import, echo=FALSE}

library('ggplot2')

```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
ggplot(cars, aes(speed, dist))+
  geom_line()
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Razvan Cretu
  • 33
  • 3
  • 9
  • 1
    See questions like this: https://stackoverflow.com/questions/29696172/how-to-hold-figure-position-with-figure-caption-in-pdf-output-of-knitr . You need to use the `fig.pos` option that controls how Latex positions figures. Alternatively you might be able to make each figure a little bit smaller and have it fit better without having to go to a new page. – Marius Jan 09 '19 at 00:09
  • Yes, a fully reproducible example might help anyone who want to answer your question. – byouness Jan 09 '19 at 09:27
  • @byouness i followed with a simple example of a R markdown file generating the same output. Please check it up. Thanks – Razvan Cretu Jan 09 '19 at 11:48
  • @Marius Already tried it, doesn't help to much as it is for other syntax (Rnw) its not for rmarkdown files (Rmd) and they don't always work togheter. Thanks for you interest anyways :) – Razvan Cretu Jan 09 '19 at 11:50
  • 1
    As @Marius said use `fig.pos`. It is not an Rnw exclusive option. If you include the `float`package (LaTeX) you can additionally use `fig.pos = H`which positions the figure "right here". Anyhow, plot in your first screenshot does not fit to the bottom of the page and therefore positioning it there does not work and LaTeX moves it to the next page. – Martin Schmelzer Jan 09 '19 at 15:06
  • @MartinSchmelzer You're right, it's not exclusively Rnw command, but I've already tried it and nothing happend.. Maybe a reproducible example could help ? How can I change my current code to place my plots in the correct position, right after the paragraph ? – Razvan Cretu Jan 09 '19 at 15:59
  • As I said, the plot only jumps to the next page, because it does not fit underneath the paragraph. So make it fit using either `fig.height` or scale it using `out.width`. – Martin Schmelzer Jan 09 '19 at 16:33

0 Answers0