如何使用 ggplot_build 和 ggplot_gtable 调整 facet_grid 框架和箱线图之间的距离

How to adjust the distance between the facet_grid frame and boxplots using ggplot_build & ggplot_gtable

我们使用箱线图呈现结果数据,并使用 facet_grid 与 ggplot2 和 geom_boxplot.

对不同方法进行分组

我们想在箱线图和 facet_grid 的框架之间添加更多 space,如下图所示。

我们使用的代码包括 ggplot_build 和 gglot_table。 需要设置 ggplot_build 的哪个参数才能在面板中获得更多 space?

require(ggplot2)
require(grid)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)), 
             data.frame(approach=1,product=2,value=seq(5,15,0.3)), 
             data.frame(approach=1,product=3,value=seq(5,17,0.2)), 
             data.frame(approach=2,product=1,value=seq(1,13,0.3)), 
             data.frame(approach=2,product=2,value=seq(3,18,0.5)), 
             data.frame(approach=2,product=3,value=seq(4,25,0.7)), 
             data.frame(approach=3,product=1,value=seq(1,15,0.6)), 
             data.frame(approach=3,product=2,value=seq(3,16,0.5)), 
             data.frame(approach=3,product=3,value=seq(1,10,0.1)))

dat$product<-as.factor(dat$product)

gg1<-ggplot(dat, aes(x =product, y = value)) +
  geom_boxplot() + 
  facet_grid(cols=vars(approach)) 


gt = ggplot_gtable(ggplot_build(gg1))
grid.draw(gt)
ggplot(dat, aes(x =product, y = value)) +
  geom_boxplot() + 
  coord_cartesian(xlim = c(1.2, 2, 2.8)) +
  facet_grid(cols=vars(approach))