使用 grid.arrange 时如何减少 ggplot 和下一个文本之间的 space?

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

考虑以下 R markdown 页面

```{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....

当我用 gridExtra 包的 grid.arrange() 绘制两个直方图 hist1hist2 时,我发现绘图太大,我想降低高度。

grid.arrange() 中的 heights 参数确实降低了高度,但它在图和下一个文本之间留下了一点空白。
如何在没有空白的情况下降低高度?

在 rmarkdown 中,我们可以在块选项中设置图形大小。

```{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

注意:查看其他块选项的摘要here