Problem description
I have created an gtable
(also gTree
grob
gDesc
) object myobj
via myobj <- gridExtra::grid.arrange(g1,g2)
from two ggplot
objects g1
,g2
some time ago and now I have to restore the data that I have used to create both ggplots. Is there a way to do this properly?
What I've tried so far
I have already tried to convert myobj
using various functions, e.g., ggpubr::as_ggplot
, resulting in an object with a waiver()
as $data
entry - so no success there - and I have also swept all the grobs
entries in myobj
where I in fact found the data points in the plot (looking like this
grobs.grobs.children.geom_point.points.5415.x1
0.04545455
), which are, however, only the position coordinates $\in (0,1)$ w.r.t. the corresponding axis. Then I probably can get the axis + the axis range and then extrapolate the original data points. But that seems excessively laborious. Is there a more simple solution to this?
Reprex (sort of)
Not sure if this actually results in the same object as I have (because mine is almost 2y old), but for a start:
library(ggplot)
# plot 1
g1 <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(cyl))
# plot 2
g2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + facet_grid(vars(cyl))
# create object
myobj <- gridExtra::grid.arrange(g1, g2, ncol=1)
# Now I would need some extract_data function to retrieve mpg and mtcars:
list_with_mpg_and_mtcars <- extract_data(myobj)