Plot to multiple devices

Share:

Description

Sends the same plot to one or several devices in a uniform manner. Helpful for preparing a script that can be configured to produce graphics in several different formats, e.g. small bitmaps for web pages, large bitmaps for slides, and pdf, eps or wmf for inclusion in documents.

Usage

1
2
plotf(theplot, file="plot%03d", type=c("active"),
      size=c(6,4,10,96), prepare=a.resetplotparams, ...)

Arguments

theplot

An object representing the plot. This can either be an expression such as expression(plot(x), points(pp)) or a Lattice object such as xyplot(y~x). The object will be eval-uated (if an expression) or print-ed (if a Lattice object) once on each device to be used. Note that this repeated processing may be inefficient if the plotting process itself (rather than just the resulting plot) is extremely complex. In such (rare) cases, you may want to consider manually using dev.print instead. plotf uses repeated processing rather than dev.print to ensure that the plotting function works as intended even if it discriminates between different devices.

file

The basename of the file to be produced. A suitable suffix of .eps, .ps, .pdf, .png, .jpg, or .wmf, respectively, will be appended for the actual files produced. The filename is ignored for the "active" device.

type

A character vector in which each entry represents one device to be used. Allowed values are "active", "current", "eps", "ps", "pdf", "png", "jpg", "jpeg", "wmf" and they represent the use of dev.cur, dev.cur, dev.copy2eps, postscript, pdf, png, jpeg, jpeg, and win.metafile, respectively. "eps" is ignored unless "active" (or "current") is also present.

size

A numeric vector of 1 to 4 components to which missing values will be added from the default value. Components 1 and 2 are width and height (in inches) of the plot to be produced. They will be supplied to the devices' width and height arguments, respectively. Component 3 is the base character size and will be supplied to the pointsize argument. For the bitmap devices, it will be corrected for resolution (which is indicated (in dpi) by component 4), because those devices always assume a resolution of 72dpi. Component 4 will also be supplied as the res argument to the bitmap devices.

prepare

A function without parameters that will be called each time after a new device has been opened and before the plot is produced.

...

All further arguments will be handed over to each(!) of the device opening functions

Details

The function uses on.exit to close a device correctly even if the plot function fails. png is almost always preferable to jpeg. Note that expressions can contain multiple calls by separating them with commas.

Value

invisible()

Author(s)

Lutz Prechelt prechelt@inf.fu-berlin.de

See Also

a.resetplotparams, dev.cur, dev.copy2eps, postscript, pdf, png, jpeg.

Examples

1
2
3
4
  x = runif(40)*4
  y = x + runif(40)
  plotf(expression(plot(x, y, type="p"))) # plots to active device only
  plotf(xyplot(y~x), "myplot", type=c("active", "png", "pdf"), size=c(3,3))