如何摆脱RMD中grid.arrange函数的空白

时间:2017-10-25 17:26:05

标签: r whitespace r-markdown gridextra

所以我正在生成2个ggplots然后使用Grid.arrange在rmd文档中并排绘制它们。为了做到这一点,我必须在grid.arrange函数中将ncol =2设置为参数。问题是,当我这样做时,图表很长。

library(ggplot2)
library(gridExtra)
x1 = ggplot(data = mtcars, aes(mtcars$disp)) +
  geom_histogram()
x2 = ggplot(data = mtcars, aes(mtcars$mpg)) +
  geom_histogram()
# worse Graph without whitespace
grid.arrange(x1,x2, ncol =2)

所以我尝试通过高度参数手动降低高度并设置heights = c(2,2)来修复它。但是当我这样做时,底部会有过多的空白。

grid.arrange(x1,x2, ncol =2, heights = c(2,2))

如何摆脱这个空白?我在RMD工作,正在为课堂报告创建输出。这个问题在R中的绘图控制台和rmd文件的pdf输出中都是可重现的。

1 个答案:

答案 0 :(得分:1)

在r chunk中使用fig.height

予。没有fig.height

```{r}
library(ggplot2)
library(gridExtra)
x1 = ggplot(data = mtcars, aes(mtcars$disp)) +
geom_histogram()
x2 = ggplot(data = mtcars, aes(mtcars$mpg)) +
geom_histogram()
# worse Graph without whitespace
grid.arrange(x1,x2, ncol =2)
```

II。使用fig.height

```{r fig.height= 3}
library(ggplot2)
library(gridExtra)
x1 = ggplot(data = mtcars, aes(mtcars$disp)) +
geom_histogram()
x2 = ggplot(data = mtcars, aes(mtcars$mpg)) +
geom_histogram()
# worse Graph without whitespace
grid.arrange(x1,x2, ncol =2)
```

针织HTML中的输出差异

enter image description here