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
?
First off a grob is just short for “grid graphical object” from the low-level graphics package grid; Think of it as a set of instructions for create a graphical object (i.e. a plot). The graphics library underneath all of ggplot2's graphical elements are really composed of grob's because ggplot2 uses grid underneath.
A grid graphical object (“grob”) is a description of a graphical item. These basic classes provide default behaviour for validating, drawing, and modifying graphical objects.
grid. arrange() function sets up a gtable layout to place multiple grobs on a page. It is located in package "gridExtra".
5 The gridExtra package. The gridExtra package provides useful extensions to the grid system, with an emphasis on higher-level functions to work with grid graphic objects, rather than the lower-level utilities in the grid package that are used to create and edit specific lower-level elements of a plot.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With