Basic skills in R: Basic graphics
Summary of low-level plotting functions
Sometimes the high-level plotting functions don’t produce exactly the kind of plot you desire. In this case, low-level plotting functions can be used to add extra information (such as points, lines or text) to the current plot. Some of the useful low-level plotting functions are listed below.
points(x, y) lines(x, y) |
These plotting functions add points or connected lines to the current plot. The argument type can also be passed to these functions, and defaults to "p" for points() and "l" for lines() . |
||||||||||
text(x, y, \(\phantom{abcd}\) labels, \(\phantom{abcd}\) ...) |
The function text() adds text to a plot at points given by x , y . Normally labels is an integer or a character vector in which case labels[i] is plotted at point (x[i], y[i]) . The default is 1:length(x) . |
||||||||||
abline(a, b) abline(h = y) abline(v = x) abline(lm.obj) |
The function abline() adds a line of intercept a and slope b to the current plot. The argument 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, \(\phantom{polygons}\) ...) |
This function 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, \(\phantom{legend}\) legend, \(\phantom{legend}\) ...) |
This function 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:
|
||||||||||
title(main, \(\phantom{titles}\) sub) |
This low-level plotting function 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 \(\phantom{axis}\) ...) |
This low-level plotting function 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. This functionis useful for adding custom axes after calling plot() with the argument axes = FALSE . |
Unlock full access