12
b <- ggplot(cars,aes(x=speed,y=dist))+geom_line()
grid.arrange(
    b,
    plot(cars),
    ncol=1
)

gives me the following error

Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList"

Let's assume my second graph has to come out of the plot function. How would one convert that output to a grob-like object so it plays nicely with grid.arrange ?

Chapo
  • 2,563
  • 3
  • 30
  • 60

1 Answers1

10

you can try with gridGraphics

library(gridGraphics)

grab_grob <- function(){
  grid.echo()
  grid.grab()
}

plot(cars)
g <- grab_grob()
b <- ggplot(cars,aes(x=speed,y=dist))+geom_line()
grid.arrange(
  b,g,
  ncol=1
)

or, alternatively, use gridBase.

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thank you for the answer. I ended up redoing the plot method with `ggplot2` because using your method I had sizing issues. I'll validate the nonetheless as it does what it's supposed to. – Chapo Nov 23 '15 at 03:11