Placing Text in Plots

These examples demonstrate how to place text in a plot.

Once a plot has been created, par( "usr" ) returns a vector with the extreme coordinates of the user’s plotting region. These can be used to place text and legends into a corner of a plot. The coordinates are, in order, xleft, xright, ybottom, and ytop. Note the use of the adj parameter in the calls to the text() function; these adjust the position of the text.

This example, which places text in the corners of the plot, is derived from code posted to the R-help list by Marc Schwartz.

##  Open a new default device.

get( getOption( "device" ) )()

##  Create the plot. This sets the "usr" coordinates.

plot( 1:10 )

##  par( "usr" ) returns a vector containing xleft, xright, ybottom, ytop.

usr <- par( "usr" )

##  Place the text. Note the use of the adj parameter.
##  Left top corner:

text( usr[ 1 ], usr[ 4 ], "left top",     adj = c( 0, 1 ), col = "blue" )

##  Left bottom corner:

text( usr[ 1 ], usr[ 3 ], "left bottom",  adj = c( 0, 0 ), col = "blue" )

##  Right top corner:

text( usr[ 2 ], usr[ 4 ], "right top",    adj = c( 1, 1 ), col = "blue" )

##  Right bottom corner:

text( usr[ 2 ], usr[ 3 ], "right bottom", adj = c( 1, 0 ), col = "blue" )

The output is:

Example of placing text in the corners of a plot

Example of placing text in the corners of a plot

This example, derived from code posted to the R-help list by Robert Baer, plots identifiers of the points using text().

##  Open a new default device.

get( getOption( "device" ) )()

##  Make test data.

x <- 1:10
y <- 10:1
names( y ) <- letters[ 1:length( y ) ]

##  Create the plot without plotting the points.

plot( x = x, y = y, pch = '', xlab = 'X',  ylab = 'Y' )

##  Write text to the position of each point.

text( x = x, y = y, labels = names( y ), cex = 0.8, col = "blue" )

The output is:

Example of labelling points in a plot

Example of labelling points in a plot

For an elaborate example of plotting letters in color instead of points, see the example about principal component analysis of the iris3 data.

See the cnvrt.coords() function in Greg Snow’s TeachingDemos package for more information about coordinate systems in plots and for examples of drawing anywhere in a plot.

library( package = TeachingDemos )
get( getOption( "device" ) )()
par( ask = TRUE )
example( cnvrt.coords )

This example of adjusting the position of text by fractions of character size is taken from Data Analysis and Graphics Using R by John Maindonald and John Braun, p. 18.

##  Load the data.

library( package = "DAAG" )
data( primates )

##  Create the plot.

plot(
    x    = primates$Bodywt,
    y    = primates$Brainwt,
    pch  = 16,
    xlab = "Body weight (kg)",
    ylab = "Brain weight (g)",
    xlim = c( 0, 300 ),
    ylim = c( 0, 1500 ) )

##  Get character width and height in user coordinates.

chw <- par()$cxy[ 1 ]  ##  character width
chh <- par()$cxy[ 2 ]  ##  character height

##  Place the text next to the points, adjusting the vertical positions
##  slightly for Rhesus monkey and Potar monkey.

text(
    x      = primates$Bodywt + chw,
    y      = primates$Brainwt + c( -0.1, 0, 0, 0.1, 0 ) * chh,
    labels = row.names( primates ),
    adj    = 0 )

The output is:

Example of labelling points in a plot

Example of labelling points in a plot