0
library(ggplot2)
library(gridExtra)

p1 <- ggplot(mtcars, aes(disp, wt)) + geom_point()
p2 <- ggplot(mtcars, aes(wt, disp)) + geom_point()
p3 <- grid.arrange(p1, p2)

Running the code grid.arrange(p1, p2) draws a nice plot of p1 and p2 beside each other. I tried to declare this as p3. But if I run p3 I don't get the same nice plot that the grid.arrange(p1, p2) gives me. I instead get what's shown below. What's wrong? I want to store grid.arrange(p1, p2) as a neat variable like p3.

p3

TableGrob (2 x 1) "arrange": 2 grobs
  z     cells    name           grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]
zx8754
  • 52,746
  • 12
  • 114
  • 209
stackinator
  • 5,429
  • 8
  • 43
  • 84
  • 2
    `grid::grid.draw(p3)` (ps if you look at the code for `grid.arrange` you see that it calls `arrangeGrob` - which is the plot gtable object - and then uses `grid.draw` to plot it.) – user20650 Jan 04 '18 at 21:47

1 Answers1

0

You can try using grid::grid.draw(p3) as in:

library(ggplot2)
library(gridExtra)

p1 <- ggplot(mtcars, aes(disp, wt)) + geom_point()
p2 <- ggplot(mtcars, aes(wt, disp)) + geom_point()
p3 <- grid.arrange(p1, p2)
grid::grid.draw(p3)

enter image description here

steveb
  • 5,382
  • 2
  • 27
  • 36
  • 2
    I *think* [this advice](https://stackoverflow.com/questions/29062766/store-output-from-gridextragrid-arrange-into-an-object#comment51466189_29072703) is still relevant. – user20650 Jan 04 '18 at 21:53
  • @user20650 Thanks for the link. You should post your answer and I should delete mine. I will delete mine after you see this comment and post your answer. You may also want to point out to not use `plot` – steveb Jan 04 '18 at 21:54