8

I need to position several plots below each other and I want the widths of the margin and figure regions to be identical, so they align neatly. Note, that I need two single plots, not one joint plot. I want to save each one in a separate PNG file. I just want their structure (margin, figure region size) to be identical.

library(ggplot2)

d <- data.frame(label=c("some very longe label with lots of text", 
                        "another long label with lots of text", 
                        "short", 
                        "also short",
                        "                                     short", 
                        "                                also short"), 
                        x = 1:6)

ggplot(d[1:2, ], aes(label, x)) + geom_bar(stat = "identity")  + coord_flip()
ggplot(d[3:4, ], aes(label, x)) + geom_bar(stat = "identity")  + coord_flip()

enter image description here enter image description here

What I want is plot 2 to have the same left margin width as in plot 1, more or less like below, of course without adding extra blanks ;)

enter image description here

In base graphics I would just set par("mar") accordingly.

How can I achieve this in ggplot?

Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • please see links at this comment http://stackoverflow.com/questions/41230345/ggplot2-change-plot-areas-for-same-size-plots-in-multiplot#comment69662588_41230345 – user20650 Dec 19 '16 at 22:17
  • https://github.com/baptiste/egg#setting-panel-size – baptiste Dec 19 '16 at 22:48

1 Answers1

6

Based on the answer from the last link from the comment above, you can equate the widths of the plots. The only difference from that answer is that they are not combined.

So for your plots

library(ggplot2)
p1 <- ggplot(d[1:2, ], aes(label, x)) + geom_bar(stat = "identity")  + coord_flip()
p2 <- ggplot(d[3:4, ], aes(label, x)) + geom_bar(stat = "identity")  + coord_flip()

Equate widths of plot

library(grid)
gl <- lapply(list(p1,p2), ggplotGrob)  
wd <- do.call(unit.pmax, lapply(gl, "[[", 'widths'))
gl <- lapply(gl, function(x) {
        x[['widths']] = wd
        x})

Plot

grid.newpage(); grid.draw(gl[[1]])
grid.newpage(); grid.draw(gl[[2]])

Which produces:

Plot 1

enter image description here

Plot 2

enter image description here

Community
  • 1
  • 1
user20650
  • 24,654
  • 5
  • 56
  • 91
  • great, thanks! width is key here. Do you happen to have a good reference to the structure of the ggplot figure regions, like this one for base graphics: http://www.melissaclarkson.com/resources/R_guides/documents/figure_layout_Ver1.pdf – Mark Heckmann Dec 20 '16 at 14:41
  • @MarkHeckmann ; Baptiste really is the person to help with this. But perhaps `expose_layout` from his [`egg package`](https://github.com/baptiste/egg) would be useful. So use `p1 <- qplot(mpg, wt, data=mtcars, colour=cyl) + ggtitle("facetted plot") ; g = expose_layout(p1) ; g$widths` . Then see how the widths align with the exposed layout plot. (widths go from left to right) (also look at `names(g)` for other things to examine) – user20650 Dec 20 '16 at 15:07