I need to store a plot object in a variable. I know that I can do:
plot(rnorm(10))
obj = recordPlot()
replayPlot(obj)
But I don\'t want to
You can do that easily with ggplot2
:
require(ggplot2)
data = data.frame(x = 1:100, y = rnorm(100))
p = ggplot(data) + geom_point(aes(x, y)) + theme_classic()
print(p) # this show the plot
From ?recordPlot
:
The displaylist can be turned on and off using dev.control.
Initially recording is on for screen devices, and off for print devices.
So you need to turn on the displaylist if you want to record a plot writing to file:
win.metafile()
dev.control('enable') # enable display list
plot(rnorm(10))
obj = recordPlot()
dev.off()
replayPlot(obj)