使用 grid.layout 时未遵守宽度

widths not adhered to when using grid.layout

我正在尝试创建类似于

的布局

但是无论我为宽度设置什么,布局都会使用 Plot 面板的整个宽度,代码如下:

masterLayout <- grid.layout(
    nrow    = 4,
    ncol    = 1,
    widths  = c(1,0.6,1,1),
    heights = c(.2,.6,.1,.1))

vp1 <- viewport(layout.pos.row=1, name="title")
vp2 <- viewport(layout.pos.row=2, name="plot")
vp3 <- viewport(layout.pos.row=3, name="legend")
vp4 <- viewport(layout.pos.row=4, name="caption")

pushViewport(vpTree(viewport(layout = masterLayout, name = "master"),
                    vpList(vp1, vp2, vp3, vp4)))

如何使用 grid 包复制图像中的布局?

所以解决这个问题的一种方法是在绘制绘图视口时推动另一个视口

seekViewport("plot")
pushViewport(viewport(width=unit(0.8, "npc")))
... plot ...
popViewport()
... continue as usual

但是宽度不起作用仍然很奇怪,或者我误解了那个论点?

我不确定您是否会发现这种方法更好,但另一种选择是使用 gridExtra 包中的 grid.arrange。例如:

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

# Plot
p1 = ggplot(mtcars, aes(mpg, wt, colour=factor(carb))) +
  geom_point() +
  theme(legend.position="bottom") +
  guides(colour=guide_legend(ncol=6)) +
  labs(colour="Legend grob takes up full width of plot window")

# Legend extraction function
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

# Extract legend
leg = g_legend(p1)
p1 = p1 + theme(legend.position="none")


e = nullGrob()  # Empty grob

# Plot layout
grid.arrange(textGrob("The title grob takes up the full width of the plot window", gp=gpar(fontsize=18)),
             arrangeGrob(e, p1, e, ncol=3, widths=c(0.2,0.6,0.2)),
             leg,
             textGrob("Figure 1. The caption grob also takes up the full width of the plot window"), 
             ncol=1, heights=c(0.05,0.7,0.15,0.1))

grid.layout 生成一个矩形 table,因此对于给定的列(行),所有宽度(resp. 高度)都将相等。您在单元格内推送特定尺寸视口的方法可能是最简单的方法。

作为替代方法,您可以使用 更多 列进行布局,并指定要用于特定 grob 的单元格范围。

library(gridExtra)

gs <- lapply(1:4, function(ii) 
  grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii)))

m <- rbind(c(1,  1,  1), 
           c(NA, 2, NA),
           c(3,  3,  3), 
           c(4,  4,  4))
grid.arrange(grobs = gs, 
             layout_matrix = m,
             heights=unit(c(1,1,1,1), c("line", "null", "line")),
             widths = unit(c(0.2,0.6,0.2), "npc"))