Aligning a plot with a legend to one without


Is there an easy way to align two plots, when one has a legend and the other does not? By default, the plots don't line up since the one without the legend makes full use of the available space.


a <- ggplot(diamonds[1:100,],aes(x,y)) + geom_point()
b <- ggplot(diamonds[1:100,],aes(carat,depth,colour=cut)) + geom_point() + theme(legend.position="bottom")

plt <- list(a,b)

pushViewport(viewport(layout=grid.layout(1, 2)))
print(plt[[1]], vp=viewport(layout.pos.row=1, layout.pos.col=1))
print(plt[[2]], vp=viewport(layout.pos.row=1, layout.pos.col=2))

Yes, you can adjust the layout/elements— @baptiste's egg package overview does a really nice job of showing the different components involved:

1 Like