force a regular plot object into a Grob for use in grid.arrange
Asked Answered
T

1

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 ?

Triennial answered 20/11, 2015 at 12:2 Comment(2)
Didn't you already ask this question earlier?Knoll
Yes but without reproducible code and not well laid out. I've deleted the old one.Triennial
M
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.

Manouch answered 21/11, 2015 at 21:51 Comment(1)
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.Triennial

© 2022 - 2024 — McMap. All rights reserved.