Save a plot in an object

base graphics draw directly on a device.

You could use

1- recordPlot

2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent

Here’s a minimal example,

plot(1:10) 

p <- recordPlot()
plot.new() ## clean up device
p # redraw

## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()

## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)

Leave a Comment