【问题标题】:ggplot2 and gridExtra: reduce spacing among plots that have facet strips removedggplot2 和 gridExtra:减少删除了刻面条的图之间的间距
【发布时间】:2017-02-14 14:50:53
【问题描述】:

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

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)

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

【问题讨论】:

    标签: r ggplot2 spacing gridextra


    【解决方案1】:

    要删除与坐标区相关的所有元素,除了您设置为element_blank 的元素外,还需要将刻度边距和刻度长度设置为零。但空间将留给刻面条。将背景和文本设置为element_blank 不会影响条带的高度和宽度。为了移除这些条带,我使用了操纵 gtable 布局的函数。但是,我认为最好在地块之间留一些空白。我将一个小的绘图边距设置为 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)
    

    【讨论】:

    • 非常感谢!它非常适合玩具示例。我注意到,当我们有:data &lt;- data.frame(x = rnorm(n), y = rnorm(n), z = rep("dummy var", n), u = seq(1, n) %% 2)p01 &lt;- ggplot(data, aes(x, y)) + geom_point() + theme + labs + facet_grid(z ~ u) 时,垂直条仍然存在于更一般的情况下,而其他所有情况都与您的解决方案相同。我最初通过修改您的代码来使其工作的努力以失败告终,但我会发布答案,如果我让它工作的话。
    • 是的,在您修改后的数据和绘图中,两个方面之间有一个内置空间。查看删除该空间的编辑。