4

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 show the graphic window. So I'm trying to do this, but with no success until now.

win.metafile()
plot(rnorm(10))
obj = recordPlot()
dev.off()
replayPlot(obj) # it shows a null plot

Well, probably because when I'm doing obj = recordPlot() the plot isn't ready yet.

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
Daniel Bonetti
  • 2,306
  • 2
  • 24
  • 33

2 Answers2

7

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)
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
-1

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
Fernando
  • 7,785
  • 6
  • 49
  • 81
  • Thanks Fernando. It worked. But I have a pretty high number of plots at the moment and I need to keep with the standard plots. However, as soon as I get, I will begin the transition of all my plots to ggplot :) – Daniel Bonetti May 20 '14 at 20:10
  • It's easy to make it look like a standard plot, just check the examples with `?theme`. – Fernando May 20 '14 at 20:16