JoelKuiper
JoelKuiper

Reputation: 4720

widths not adhered to when using grid.layout

I'm trying to create a layout similar to

Example layout

But whatever I set for the widths the layout keeps using the full width of the panel for Plot with the following code:

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)))

How can I replicate the layout in the image using the grid package?

Upvotes: 4

Views: 393

Answers (3)

eipi10
eipi10

Reputation: 93761

I'm not sure if you'll find this method superior, but another option is to use grid.arrange from the gridExtra package. For example:

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))

enter image description here

Upvotes: 1

baptiste
baptiste

Reputation: 77096

grid.layout makes a rectangular table, so for a given column (row) all widths (resp. heights) will be equal. Your approach of pushing a viewport of specific dimensions inside the cell is probably the easiest.

As an alternative approach, you could make a layout with more columns, and specify which range of cells to use for a specific 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"))

enter image description here

Upvotes: 4

JoelKuiper
JoelKuiper

Reputation: 4720

So one way to fix this is to push another viewport when drawing the plot viewport

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

But it's still strange that widths does not work, or am I misinterpreting that argument?

Upvotes: 2

Related Questions