3

I need to wrap several plots in a grid, often an uneven number, so there'll often be an "empty spot". I need to use arrangeGrob() -- not grid.arrange() -- because I want to save the plot for later, not plot() it right away.

This works fine, but oddly, arrangeGrob() leaves some weird background in the empty spots.

Like so:

library(ggplot2)
p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot()
p2 <- ggplot(mtcars, aes(x=factor(cyl), y=wt)) + geom_boxplot()
p3 <- ggplot(mtcars, aes(x =factor(cyl), y=disp)) + geom_boxplot()
library(gridExtra)
y <- arrangeGrob(p1, p2, p3, ncol = 2)
plot(y)

yields a plot with some weird gray stuff in the bottom right corner:

grey weirdness

Compare this to grid.arrange():

grid.arrange(p1, p2, p3, ncol = 2)

yields a pretty plot with no grey weirdness:

enter image description here

Where does this gray stuff in the bottom right corner come from? And how do I get rid of it?

Notice that I cannot avoid the empty spots by changing ncol; I sometimes have an uneven number of plots, so there'll always be empty spots. I'm ok with empty spots, I just like them to be clean. (In isolation, that last sentence sounds pretty OCD-ish.


UPDATE

The package author (?) answered below: I should have used grid.draw(y).

A similar problem remains (maybe the same root cause?): if you plot some object before, the "empty spot" remains occupied by that past plot. Weird. Like so:

plot(p1)
grid.draw(y)

yields:

past plot weirdness

maxheld
  • 3,963
  • 2
  • 32
  • 51
  • please note that it **has to be `plot(y)`** ~~not `print(y)`~~. A recent release for `gridExtra` changed the output as per http://stackoverflow.com/questions/31458051/store-arrangegrob-to-object-does-not-create-printable-object/ **UPDATE** should be `grid.draw()` as per answer. Sorry about that. – maxheld Jul 16 '15 at 21:05
  • 1
    Whoops, guess my package is out of date. Sorry for the bad edit. – Gregor Thomas Jul 16 '15 at 21:07
  • 1
    for your follow-up question, you need to clear the page with `grid.newpage()` otherwise things are drawn on top of previous content. – baptiste Jul 16 '15 at 21:22
  • uh, thanks – must read up on things `grid`. I'm sure there are good reasons for this. I used to like how `arrangeGrob`ed and `grid.arrange`d objects could be used *exactly* like the `ggplot`s they consisted of. This makes things a bit more complicated, especially in the context of plotting functions – have to account for more possibilities now. – maxheld Jul 16 '15 at 21:39
  • 2
    ggplot recently gained a grid.draw method (which is the standard way to display grid graphics), so you could perhaps use this. I don't know why lattice set this trend of using plot or print to display graphics, but to me that's the real cause of confusion. I'd argue that anything based on grid graphics should follow the grid conventions. – baptiste Jul 16 '15 at 21:42
  • ok, that's great – so at least I can use grid.draw for everything. I'm in no position to judge the consistency / technical merit – sure you're right. – maxheld Jul 16 '15 at 21:43

2 Answers2

4

arrangeGrob() now returns a gtable, which you should draw with grid.draw(), not plot().

grid.draw(y)

yields

fine plot

To get rid of artefacts from past plots (as per above update) use grid.newpage().

maxheld
  • 3,963
  • 2
  • 32
  • 51
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • thanks, sorry I didn't know that. Turns out, however, a related / similar problem remains, as per the above update: if a plot was previously rendered, it is masked only by the occupied cells. Should I raise a new question / bug report about that? – or is it (again) my mistake? – maxheld Jul 16 '15 at 21:21
-1

Remove the nrow argument. Like so:

library(ggplot2)
p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot()
p2 <- ggplot(mtcars, aes(x=factor(cyl), y=wt)) + geom_boxplot()
library(gridExtra)
y <- arrangeGrob(p1, p2, ncol = 2)
plot(y)
theamateurdataanalyst
  • 2,794
  • 4
  • 38
  • 72
  • I know — I don't think this solves my problem, because I **cannot** avoid the empty spots completely in my use case. The nrow and ncol are only to illustrate. – maxheld Jul 16 '15 at 20:19
  • thanks, again. Maybe my question was a bit unclear: the white space *is not my problem* –- it is also unavoidable because I often have an uneven number of plots (see above edited example). My problem is only the weird grey stuff in the bottom right corner. – maxheld Jul 16 '15 at 20:56