plotAndSave: Display And Save A Plot

View source: R/plotAndSave.R

plotAndSaveR Documentation

Display And Save A Plot


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


plotAndSave(plot.func,, ..., folder=getwd(),
         format=c("eps", "pdf"),
         options=list(eps = list(onefile=TRUE, horizontal=FALSE,
                                 width=7, height=7),
                      ps  = list(onefile=TRUE, horizontal=FALSE,
                                 width=7, height=7),
                      pdf = list(onefile=TRUE)),
         do.plot=TRUE, do.return=do.plot)



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

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.


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


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.


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.


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


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


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.


the return value of the plotting function.


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.


Thorn Thaler

See Also

pdf, postscript, jpeg, png, bmp, tiff


## Not run: 
## Plotting Function
# For 'lattice' graphics:
# f <- function(x, ...) xyplot(x~sin(x), ...)
# 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)

ttutils documentation built on April 5, 2022, 1:15 a.m.

Related to plotAndSave in ttutils...