Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot to multiple devices at the same time?

Tags:

plot

r

ggplot2

When I am plotting, I often plot to an eps file and a png file like this:

postscript(file=paste(dir, output, "_ggplot.eps", sep=""), onefile=FALSE, horizontal=FALSE, width=4.8, height=4.0)
# Plotting code
dev.off()

png(paste(dir, output, "_ggplot.png", sep=""), width=450, height=300)
# Plotting code
dev.off()

The problem is that the plotting code is repeated twice. Is it possible to specify multiple devices for plotting?

like image 492
Legend Avatar asked Oct 30 '11 00:10

Legend


People also ask

How do I plot multiple graphs in R?

We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.

Which plotting method is used for current graphics device?

The Direct Graphics system in IDL offers a variety of ways to create plots of your data. Two-dimensional graph types include line, scatter, polar, bar, histogram, and contour plots.

What does Dev OFF () do in R?

dev. off shuts down the specified (by default the current) device. If the current device is shut down and any other devices are open, the next open device is made current.

What is the default number for screen graphics device?

As you might have guessed, every open graphics device is assigned an integer greater than or equal to 2. You can change the active graphics device with dev.


2 Answers

You can combine them using dev.copy(). For example,

  X11 ()
  plot (x,y)
  dev.copy (jpeg,filename="test.jpg");
  dev.off ();

Lookup help(dev.copy) for more details.

Usage:

     dev.copy(device, ..., which = dev.next())
     dev.print(device = postscript, ...)
     dev.copy2eps(...)
     dev.copy2pdf(..., out.type = "pdf")
     dev.control(displaylist = c("inhibit", "enable"))
like image 91
Itamar Avatar answered Oct 17 '22 18:10

Itamar


No it's not possible. At least not according to the manual for ?grDevices:

"Details: Only one device is the ‘active’ device: this is the device in which all graphics operations occur. There is a "null device" which is always open but is really a placeholder: any attempt to use it will open a new device specified by getOption("device"))."

like image 30
Tyler Rinker Avatar answered Oct 17 '22 19:10

Tyler Rinker