Kickstarting R - Adding text to a plot

Text labels

The text() function allows us to put text on the plot where we want it. An obvious use is to label a line or group of points.

text(c(2,2),c(37,35),labels=c("Non-case","Case"))

This would give us two labels in the upper right corner of the plot, which with appropriate arrows could be used in place of a legend. Note that vectors were supplied as the coordinates. The first label would be centered at (2,37), and the second at (2,35), as the default alignment is to center the labels.

Text labels can also be placed in the margins of a plot using the mtext() function.

> mtext(c("Low","High"),side=1,line=2,at=c(5,7))

This would place the words "Low" and "High" on the second line below the X axis centered at 5 and 7 units. Note that the documentation declares that "user coordinates" in mtext() do not match those on the plot, but it appears that they do at the moment (R-1.6.2).

For more information, see An Introduction to R: Low-level plotting commands.

Back to Table of Contents