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.

Low-level plotting commands

Sometimes the high-level plotting functions don't produce exactly the kind of plot you desire. In this case, low-level plotting commands can be used to add extra information (such as points, lines or text) to the current plot.

Some of the more useful low-level plotting functions are:

points(x, y)
lines(x, y)

Adds points or connected lines to the current plot. plot()'s type= argument can also be passed to these functions (and defaults to "p" for points() and "l" for lines().)

text(x, y, labels, …)

Add text to a plot at points given by x, y. Normally labels is an integer or character vector, in which case labels[i] is plotted at point (x[i], y[i]). The default is 1:length(x).

Note: This function is often used in the sequence

> plot(x, y, type="n"); text(x, y, names)

The graphics parameter type="n" suppresses the points but sets up the axes, and the text() function supplies special characters, as specified by the character vector names for the points.

abline(a, b)
abline(h=y)
abline(v=x)
abline(lm.obj)

Adds a line of slope b and intercept a to the current plot. h=y may be used to specify y-coordinates for the heights of horizontal lines to go across a plot, and v=x similarly for the x-coordinates for vertical lines. Also lm.obj may be list with a coefficients component of length 2 (such as the result of model-fitting functions,) which are taken as an intercept and slope, in that order.

polygon(x, y, …)

Draws a polygon defined by the ordered vertices in (x, y) and (optionally) shade it in with hatch lines or fill it if the graphics device allows the filling of figures.

legend(x, y, legend, …)

Adds a legend to the current plot at the specified position. Plotting characters, line styles, colors, etc., are identified with the labels in the character vector legend. At least one other argument v (a vector the same length as legend) with the corresponding values of the plotting unit must also be given as follows:

legend( , fill=v)

Colors for filled boxes

legend( , col=v)

Colors in which points or lines will be drawn

legend( , lty=v)

Line styles

legend( , lwd=v)

Line widths

legend( , pch=v)

Plotting characters (character vector)

title(main, sub)

Adds a title main to the top of the current plot in a large font and (optionally) a sub-title sub at the bottom in a smaller font.


axis(side, …)

Adds an axis to the current plot on the side given by the first argument (1 to 4, counting clockwise from the bottom.) Other arguments control the positioning of the axis within or beside the plot, and tick positions and labels. Useful for adding custom axes after calling plot() with the axes=FALSE argument.

Low-level plotting functions usually require some positioning information (e.g., x and y coordinates) to determine where to place the new plot elements. Coordinates are given in terms of user coordinates which are defined by the previous high-level graphics command and are chosen based on the supplied data.

Where x and y arguments are required, it is also sufficient to supply a single argument being a list with elements named x and y. Similarly, a matrix with two columns is also valid input. In this way, functions such as locator() (see below) may be used to specify positions on a plot interactively.

  • Mathematical annotation
  • Hershey vector fonts


Mathematical annotation

In some cases, it is useful to add mathematical symbols and formulae to a plot. This can be achieved in R by specifying an expression rather than a character string in any one of text, mtext, axis, or title. For example, the following code draws the formula for the Binomial probability function:

> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x})))

More information, including a full listing of the features available, can be obtained from within R using the commands:

> help(plotmath)
> example(plotmath)
> demo(plotmath)


Hershey vector fonts

It is possible to specify Hershey vector fonts for rendering text when using the text and contour functions. There are three reasons for using the Hershey fonts:

  • Hershey fonts can produce better output, especially on a computer screen, for rotated and/or small text.
  • Hershey fonts provide certain symbols that may not be available in the standard fonts. In particular, there are zodiac signs, cartographic symbols, and astronomical symbols.
  • Hershey fonts provide cyrillic and japanese (Kana and Kanji) characters.

More information, including tables of Hershey characters, can be obtained from within R using the commands:

> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)