在grid.arrange图中出现奇怪的空格

7

我正试图给grid.arrange图表添加脚注。我在这个可重复的例子中展示了我的想法:

library(ggplot2)
library(gridExtra)
library(grid)
library(gtable)

summary(anscombe)

p1 <- ggplot(anscombe) + geom_point(aes(x1, y1), color = "darkorange", size = 3) + theme_bw() 

p2 <- ggplot(anscombe) + geom_point(aes(x2, y2), color = "darkorange", size = 3) + theme_bw()

p3 <- ggplot(anscombe) + geom_point(aes(x3, y3), color = "darkorange", size = 3) + theme_bw()

p4 <- ggplot(anscombe) + geom_point(aes(x4, y4), color = "darkorange", size = 3) + theme_bw() 

title <- textGrob("Some title",
              gp=gpar(fontsize=20,fontface=2))

source1<- textGrob("Source: https://rpubs.com/neilfws/91339",
              hjust=0,x=0,y=1,
              gp=gpar(fontsize=10,fontface=3))

grid.arrange(arrangeGrob(p1,p2,p3,p4, ncol=2, sub = source1), top = title)

这段代码生成了下面那张图片:

enter image description here

在图表下方有大量空白。如何消除这个问题?为什么会出现呢?
2个回答

4

尝试使用bottom代替sub

grid.arrange(arrangeGrob(p1,p2,p3,p4, ncol=2, bottom = source1), top = title)

enter image description here


1
不错。简化一下,grid.arrange(p1,p2,p3,p4, ncol=2, bottom = source1, top = title) 也可以。 - Josh O'Brien
1
是的,白色空间的原因是它将字幕视为另一个要排列的grob,从而创建了一个3x2的布局(默认情况下高度相等)。这是具有“...”作为第一个参数的函数的问题,它们会默默地忽略不匹配以下参数的名称。 - baptiste

0

一种简单的方法是使用以下代码:

grid.arrange(arrangeGrob(p1,p2,p3,p4, ncol=2, sub = source1), top = title, heights = c(50,-15))

并且修改高度,直到您获得所需的间距。我只通过设置显示值的参数来实现这一点:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接