Display And Save A Plot

Description

plotAndSave saves a plot as “pdf”, “(e)ps”, “jp(e)g”, “png”, “bmp”, “tiff”, “emf” and/or “wmf” and additionally displays the plot.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
plotAndSave(plot.func, plot.name, ..., folder=getwd(),
         format=c("eps", "pdf"),
         options=list(eps = list(onefile=TRUE, horizontal=FALSE,
                                 paper="special",
                                 width=7, height=7),
                      ps  = list(onefile=TRUE, horizontal=FALSE,
                                 paper="special",
                                 width=7, height=7),
                      pdf = list(onefile=TRUE)),
         do.plot=TRUE, do.return=do.plot)

Arguments

plot.func

either a function or a non-empty character string naming the plotting function to be called.

plot.name

a character string (without any suffix such as “.pdf” or “.eps”) giving the name of the file where the plot should be saved to.

...

additional arguments to be passed to the plotting function.

folder

a character string giving the name of the folder to which the plot should be saved. The default is the current directory.

format

output format. Must be a subset of (“pdf”, “(e)ps”, “jp(e)g”, “png”, “bmp”, “tiff”, “emf”, “wmf”). The latter two can be used only on with a Windows OS. The default is to produce both an eps-file and a pdf-file. Can be abbreviated.

options

named list of options to be passed to the respective device driver. Each entry of the list is an option list for the device corresponding to the name of the list item.

do.plot

logical. If TRUE (the default) the plot is displayed.

do.return

logical. If TRUE the return value of the plotting function is returned. Defaults to the value of the parameter do.plot.

Details

The purpose of this function is to produce a plot on the monitor and to save it to a file simultaneously.

The file name must be given without any file-suffix. Depending on the argument format the function then generates the respective file with the appropriate suffix. The path should not be included in the file name, since the location where the files should be saved to is controlled by the parameter folder.

The function needs a plotting function to be defined, which actually does the plotting itself. Additional arguments (e.g. further graphical parameters) can be passed to plotAndSave, which in turn, passes these arguments down to the plotting function,

The parameters of devices are controlled by the arguments options.

Value

the return value of the plotting function.

Note

When using Trellis plots from package lattice one has to assure that the plotting function actually does the plotting. Since the default behaviour of Trellis plots is just to return the Trellis object, one should wrap the call to the particular lattice function in a call of the function print. The generic function print ensures that the plot is displayed and not just returned as an object.

Author(s)

Thorn Thaler

See Also

pdf, postscript, jpeg, png, bmp, tiff

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
## Not run: 
## Plotting Function
# For 'lattice' graphics:
# WRONG:
# f <- function(x, ...) xyplot(x~sin(x), ...)
# CORRECT:
# f <- function(x, ...) print(xyplot(x~sin(x), ...))

f <- function(x, ...) plot(x, sin(x), col=2, type="l", ...)

# Save the plot as "Sine_Function.pdf" in the current folder
# and add a title to the plot

plotAndSave(f, "Sine_Function", x=seq(-pi, pi, length=100),
            main="Sine-Function", format="pd")


## End(Not run)