ggplot2 和 gridExtra:减少已移除小平面条的绘图之间的间距

ggplot2 and gridExtra: reduce spacing among plots that have facet strips removed

我希望绘制许多间隔紧密的图形,如下面的玩具示例所示:

library(ggplot2)
library(gridExtra)
set.seed(314159)
n <- 100
data <- data.frame(x = rnorm(n), y = rnorm(n), z = rep("dummy var", n))

p00 <- ggplot(data, aes(x)) + stat_density() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p10 <- ggplot(data, aes(y, x)) + geom_point() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p11 <- ggplot(data, aes(y)) + stat_density() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)

grid.arrange(p00, p01, p10, p11, ncol = 2)

尽管我尽了最大的努力,但我无法克服在从我的图表中删除小平面条带后尝试这样做时出现的复杂情况。在下面的示例中,我通过虚拟变量的分面向每个图形添加了水平和垂直条带:

p00 <- p00 + facet_grid(z ~ z)
p01 <- p01 + facet_grid(z ~ z)
p10 <- p10 + facet_grid(z ~ z)
p11 <- p11 + facet_grid(z ~ z)

grid.arrange(p00, p01, p10, p11, ncol = 2)

接下来,我根据 this post 中概述的程序移除条带。然而,相比之下,结果图的间距相当大:

p00 <- p00 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p01 <- p01 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p10 <- p10 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p11 <- p11 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())

grid.arrange(p00, p01, p10, p11, ncol = 2)

任何关于如何减少图表间距的建议都将不胜感激。

要删除与轴关联的所有元素,除了您已设置为 element_blank 的元素外,还需要将刻度边距和刻度长度设置为零。但是 space 将保留用于小平面条带。将背景和文本设置为 element_blank 不会影响条带的高度和宽度。为了移除条带,我使用了操纵 gtable 布局的函数。不过,我觉得在地块之间留一些白色的space比较好。我已将小图边距设置为 0.2 行。

library(ggplot2)
library(gridExtra)
set.seed(314159)
n <- 100
data <- data.frame(x = rnorm(n), y = rnorm(n), z1 = rep("dummy var", n), z2 = rep("dummy var", n))

theme = theme(plot.margin = unit(c(.2,.2,.2,.2), units = "lines"), 
         axis.text = element_blank(), 
         axis.title = element_blank(), 
         axis.ticks = element_blank(), 
         axis.ticks.length = unit(0, "lines"))
labs = labs(x = NULL, y = NULL) 

p00 <- ggplot(data, aes(x)) + stat_density() + theme + labs + facet_grid(z1 ~ z2)
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme + labs + facet_grid(z1 ~ z2)
p10 <- ggplot(data, aes(y, x)) + geom_point() + theme + labs + facet_grid(z1 ~ z2)
p11 <- ggplot(data, aes(y)) + stat_density() + theme + labs + facet_grid(z1 ~ z2)

这里是操作gtable布局的地方。

# Get the four gtables (and the four plots) into a list
pList = list(p00, p01, p10, p11)
gList = lapply(pList, ggplotGrob)

# Remove the top strip from each plot
stripT <- subset(gList[[1]]$layout, grepl("strip-t", gList[[1]]$layout$name))
gList = lapply(gList, function(x) x[-stripT$t, ])

# Remove the right strip from each plot
stripR <- subset(gList[[1]]$layout, grepl("strip-r", gList[[1]]$layout$name))
gList = lapply(gList, function(x) x[, -stripR$r])

# Draw the revised plots
nCol <- floor(sqrt(length(pList)))
do.call(grid.arrange, c(gList, ncol = nCol))

编辑: 使用修改后的数据和绘图。

library(grid)

data <- data.frame(x = rnorm(n), y = rnorm(n), z = rep("dummy var", n), u = seq(1, n) %% 2) 
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme + labs + facet_grid(z ~ u)

g = ggplotGrob(p01)
stripT = subset(g$layout, grepl("strip-t", g$layout$name))
g = g[-stripT$t, ]
stripR = subset(g$layout, grepl("strip-r", g$layout$name))
g = g[, -stripR$r]

grid.draw(g) # Still got the space between the facets

g$widths  # where is the space? it's the 5.55 pt width

g$widths[[5]] = unit(0, "lines") # remove it completely
g$width
grid.draw(g)