tenticon
tenticon

Reputation: 2913

How to reduce space between ggplot and next text when using grid.arrange?

Consider the following R markdown page

```{r setup, include=FALSE}
require(ggplot2)
require(data.table)
require(gridExtra)
require(knitr)

knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}
df <- data.table(x1=rnorm(n = 100), x2=rnorm(100))
```

Here is a plot

```{r,echo=FALSE, out.width='\\textwidth'}
hist1 <- ggplot(df, aes(x=x1)) +
  geom_histogram()

hist2 <- ggplot(df, aes(x=x2)) +
  geom_histogram()

grid.arrange(hist1, hist2, ncol=2, heights=c(10,10))
```

Some more text....

When I plot the two histograms hist1 and hist2 with grid.arrange() of the gridExtra package I find the plots too large, I want to reduce the height.

The heights parameter in grid.arrange() does reduce the height but it leaves a bit whitespace between the plot and the next text.
How can I reduce the height without getting that whitespace?

Upvotes: 1

Views: 426

Answers (1)

jay.sf
jay.sf

Reputation: 72828

In rmarkdown we can set the figure sizes in the chunk options.

```{r, echo=FALSE, out.width='\\textwidth', message=FALSE, fig.height=3, fig.width=6, fig.align="center"}
df <- data.table(x1=rnorm(100), x2=rnorm(100))
hist1 <- ggplot(df, aes(x=x1)) + geom_histogram()
hist2 <- ggplot(df, aes(x=x2)) + geom_histogram()
grid.arrange(hist1, hist2, ncol=2)
```

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

YIelding

enter image description here

Note: See a summary of other chunk options here.

Upvotes: 1

Related Questions