The plot
function is the main plotting tool in R. A typical call will be plot(x, y, opt1 = val1, opt2 = val2, ...)
where opt1
, opt2
, etc are plotting options. Some useful plotting options are (see ?plot.default
and ?par
for a full list of options)
type
The type of the plot (a character). Possible values include"p"
for points (i.e. a scatterplot, the default);"l"
for lines (piecewise linear interpolant);"b"
for both points and lines;"n"
for plotting nothing. This option draws only the axes and is useful when we want to plot multiple objects on the same axes.xlim
and ylim
The limits of the axes (a vector of length 2).xlab
, ylab
, main
Titles for the axes and main title (a character string).col
* The colour of the points or lines. This can be specified in a number of ways. See relevant section below.pch
* The plot style for points (relevant if type
is "p"
or "b"
). This can be specified as a number or a single character string.lty
* The line style (relevant if type
is "l"
or "b"
). This can be specified as a number or a name.lwd
* The width of points or lines. The default value is 1. Higher numbers give thicker (bolder) points and lines.
* Graphical parameter, can also be set globally using par
.
Depending on whether x
and y
are numerical or categorical, the appropriate plot will be displayed.
x type |
y type |
Default plot |
---|---|---|
numerical | numerical | scatterplot |
categorical | numerical | boxplot |
numerical | categorical | spineplot |
categorical | categorical | spineplot |
attach(ToothGrowth) # Add ToothGrowth data to path so we can plot its variables See ?ToothGrowth for more information
plot(dose, len)
When a plotting function is called, the graph appears in what in the R language is called a "device". If a device is open, then the plot appears on that device (and may overwrite the existing plot), otherwise the default device will be opened. The user can open a plotting device before running the plotting function. This can be useful if you don't want to overwrite the existing plot or if you want to export the plot. A device is opened by calling the device function (see below) and is closed by running dev.off()
. Note that if you are plotting to a device that writes to a file, unless dev.off()
is called, the plot won't be created. Some useful device functions are (see ?Devices
for a complete list):
X11()
Opens a new plotting window. This is useful to avoid overwriting an existing plot;postscript(file = "myfig.eps", ...)
and pdf(file = "myfig.pdf", ...)
These functions are useful for exporting plots. For example to export to the eps format suitable for importing into latex a typical call will be# Uncomment lines to export plots
# postscript("myfig.eps", width = 4.0, height = 3.0, paper = "special",
# horizontal = FALSE, family = "ComputerModern", encoding = "TeXtext.enc")
plot(dose, len)
#dev.off()
Graphical parameters are options controlling different parts of the plotting device. These are global plotting options which remain active as long as the plotting device is open. They can be set using the function par
, e.g. par(opt1 = val1, opt2 = val2, ...)
. Some graphical parameters can also be specified for individual plotting calls (see e.g. the arguments to the plot
function). Some additional useful graphical parameters are
mfrow
A vector of positive integers of length 2. This is used to split the graphic device into several subplots. For example, the call par(mfrow = c(2, 3))
splits the device into 2 rows and 3 columns, the 6 plots will appear row-wise. col
The plotting colour. This can be specified as a number (1 is black, 2 is red, 3 is green, 4 is blue, etc) or as a name (type colors()
in R for a list of all names).pch
The plotting character for points. This can be specified as an integer or a length-one character string. See ?pch
for possible values.lty
The line type. This can be specified as an integer or a
character string. (0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash)When calling the function par
, a new device will be opened if none is available. A typical command sequence when wanting to set specific plotting parameters is as follows.
# Uncomment lines to export plots
# pdf(file = "myfile.pdf") # Open the plotting device
par(bg = "yellow") # Set the graphical parameter, in this case the background colour
plot(dose, len) # Create the plot
# dev.off() # Close the plotting device, necessary if exporting the plot
Here is another example which plots available R colours. It makes use of the function grep
to remove redundant names, the function polygon
to draw a coloured rectangle, and the function text
to add text to a plot.
# Uncomment lines to export plots
clall = colors()
cl = grep("gray", clall, value = TRUE, invert = TRUE) # Remove entries containing 'gray' since there are duplicates with 'grey'
ncl = length(cl) # Number of colours
ncol = 4 # Number of columns
nrow = ceiling(ncl/ncol) # Number of rows
# pdf("Rcols.pdf", width = 7, height = 30)
par(mar = c(.1, .1, .1, .1), ann = FALSE, xaxs = "i", yaxs = "i") # Conserve space
plot(c(0,ncol), c(0,nrow), type = "n", axes = FALSE) # Draw the plot area, plot nothing
for (i in 1:ncl) {
row = (i-1) %% nrow # Row position
col = ceiling(i/nrow) - 1 # Column position
polygon(c(0, 0, 1, 1) + col, c(0, 1, 1, 0) + row , col = cl[i], border = NA) # Draw a coloured box
text(.2 + col, .5 + row , cl[i], col = "black", cex = .6) # Display colour name
text(.7 + col, .5 + row, cl[i], col = "white", cex = .6) # Display colour name
}
#dev.off()
The function points
operates on an existing plot by adding new points to it. A typical call will be points(x, y, opt1 = val1, opt2 = val2, ...)
where opt1
, opt2
, etc are plotting options. Options include type
as in function plot
and the other graphical parameter options also available for plot
.
The call lines(x, y)
is a synonym for points(x, y, type = "l")
.
The function abline
is used for adding straight lines to an existing plot. A call to the function can be of the form
abline(a = 1, b = 3)
Will draw the line $y = a + b x$.abline(h = c(1, 2, 3))
or abline(v = c(-1, 0))
Will draw horizontal or vertical lines at the specified points.The function legend
adds a legend to an existing plot. A typical call to the function is
legend(x = xcoord, y = ycoord, legend = ..., opt1 = val1, opt2 = val2, ...)
legend(x = keyword, legend = ..., opt1 = val1, opt2 = val2, ...)
The position of the legend can be specified by a keyword: "top"
, "topright"
, "right"
, "center"
, etc.