Learn R Programming

ttutils (version 1.0-1.1)

plotAndSave: 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

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.

Value

the return value of the plotting function.

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.

See Also

pdf, postscript, jpeg, png, bmp, tiff

Examples

Run this code
# 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")

# }

Run the code above in your browser using DataLab