Base-R Graphics

This section introduces the base-R graphics. Reading the materials will familiarize you with different options and commands used for plotting. You should start coding by implementing the high-level function like the plot, then incrementally modify and add code to change the plot appearance and add the function par to fine-tune the margins, etc. You will also learn about the R graphics devices used to save plots for publications (do not use the point-and-click interface to save plots from RStudio); these device commands are also applicable to outputs of the ggplot2.

Using graphics parameters

When creating graphics, particularly for presentation or publication purposes, R's defaults do not always produce exactly that which is required. You can, however, customize almost every aspect of the display using graphics parameters. R maintains a list of many graphics parameters that control line style, colors, figure arrangement, and text justification, among many others. Every graphics parameter has a name (such as 'col', which controls colors) and a value (a color number, for example.)

A separate list of graphics parameters is maintained for each active device, and each device has a default set of parameters when initialized. Graphics parameters can be set in two ways: permanently, affecting all graphics functions that access the current device; or temporarily, only a single graphics function call.

  • Permanent changes: The par() function
  • Temporary changes: Arguments to graphics functions


Permanent changes: The par() function

The par() function is used to access and modify the list of graphics parameters for the current graphics device.

par()

Without arguments, returns a list of all graphics parameters and their values for the current device.

par(c("col", "lty"))

With a character vector argument, returns only the named graphics parameters (again, as a list.)

par(col=4, lty=2)

With named arguments (or a single list argument), sets the values of the named graphics parameters and returns the original values of the parameters as a list.

Setting graphics parameters with the par() function changes the value of the parameters permanently, in the sense that all future calls to graphics functions (on the current device) will be affected by the new value. You can think of setting graphics parameters in this way as setting "default" values for the parameters, which will be used by all graphics functions unless an alternative value is given.

Note that calls to par() always affect the global values of graphics parameters, even when par() is called from within a function. This is often undesirable behavior - usually, we want to set some graphics parameters, do some plotting, and then restore the original values to avoid affecting the user's R session. You can restore the initial values by saving the result  par() when making changes and restoring the initial values when plotting is complete.

> oldpar <- par(col=4, lty=2)
   plotting commands 
> par(oldpar)

To save and restore all settable graphical parameters, use

> oldpar <- par(no.readonly=TRUE)
   plotting commands 
> par(oldpar)


Temporary changes: Arguments to graphics functions

Graphics parameters may also be passed to (almost) any graphics function as named arguments. This has the same effect as passing the arguments to the par() function, except that the changes only last for the duration of the function call. For example:

> plot(x, y, pch="+")

produces a scatterplot using a plus sign as the plotting character without changing the default plotting character for future plots.

Unfortunately, this is not implemented entirely consistently, and it is sometimes necessary to set and reset graphics parameters using par().