10  Text Annotations

10.1 Introduction

In this chapter, we will learn to add text annotations. There are occasions when you want to display additional information in a plot. This is usually achieved by adding text either inside the plot or on the margins. For example, you might want to label a line/bar or add formulas to better communicate what is shown in the plot. The idea is to use the available space within/outside the plot to provide additional information that can be useful to the end users. We will learn to add text inside as well as on the margins of the plot. This is accomplished using the following two functions:

  • text() : add text inside the plot
  • mtext() : add text on the margins of the plot

10.2 Syntax

Let us take a quick look at the syntax of both the functions:

text(x, y = NULL, labels = seq_along(x$x), adj = NULL,
     pos = NULL, offset = 0.5, vfont = NULL,
     cex = 1, col = NULL, font = NULL, ...)

mtext(text, side = 3, line = 0, outer = FALSE, at = NA,
      adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...)

10.3 Text Inside the Plot

To add text inside a plot, the following arguments must be supplied to the text() function:

  • labels : the text to be displayed
  • x : x axis coordinate
  • y : y axis coordinate

Below is a simple example:

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text')

The text appears at the coordinates (340, 30). Ensure that the text is enclosed in single/double quotes and the coordinates provided are within the range of the X and Y axis variables.

10.4 Color

The color of the text can be modified using the col argument in the text() function.

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', col = 'red')

The below plot depicts the appearance of the text for different values of the col argument:

10.5 Font

The font of the text can be modified using the font argument in the text() function.

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', col = 'red', font = 2)

The below plot depicts the appearance of the text for different values of the font argument:

10.6 Font Family

The font family of the text can be modified using the family argument in the text() function.

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', col = 'red', family = 'mono')

The below plot depicts the appearance of the text for different values of the family argument:

10.7 Font Size

The font size of the text can be modified using the cex argument in the text() function.

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', col = 'red', cex = 2)

The below plot depicts the appearance of the text for different values of the cex argument:

10.8 Text on the Margins

The mtext() function allows the user to place the text on the margins of the plot. It allows the user to modify the location of the text in multiple ways and we will explore them one by one. To begin with, let us add text to the plot using the mtext() function. The minimum input you need to provide is the text itself. Below is a simple example:

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text')

As you can see, the text is placed on the margin of the plot and not inside the plot. Next, we will specify the margin on which to place the text.

10.9 Specify Margin

Use the side argument to specify the margin on which you want to place the text. If takes values 1 to 4, each representing one side of the plot.

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', side = 1)

The below plot displays the appearance of the text when differnt options for side argument are supplied:

10.10 Line

The line argument places the text at a specified distance from the margin. The default value is 0. As the value increases, the text is placed farther from the margin and outside the plot. As the value decreases, the text is placed inside the plot and farther from the margin. Below is an example where the text is placed outside the plot as the value is greater than 1.

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', line = 1)

When the value is less than 0, the line argument places the text inside the plot.

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', line = -1)

The below plot displays the appearance of the text when different values are supplied to the line argument:

10.11 Alignment

The adj argument is used for horizontal alignment of the text. It takes values between 0 and 1. If set to 0, the text will be left aligned and at 1, it will be right aligned. Below is an example where the text is left aligned as adj is set to 0.

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', adj = 0)

When the value is set to 1, the text is right aligned.

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', adj = 1)

The below plot displays the appearance of the text when different values are supplied to the adj argument:

10.12 Position

The pos argument of the text() function places the text relative to the specified coordinates without needing adj. It takes values 1 to 4, each representing a position around the point:

  • 1: below
  • 2: to the left
  • 3: above
  • 4: to the right
A grid of scatterplots of displacement against miles per gallon. In each panel the same single labelled point is annotated, but the label is placed in a different compass position relative to it, stepping through south-west, west, north-west, north and further around.
Figure 10.1: The pos argument placing text relative to a point

10.13 Offset

The offset argument of the text() function controls how far the text is placed from the coordinates when pos is used. The distance is measured in character widths and the default value is 0.5.

plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', pos = 3, offset = 1.5)
A scatterplot of displacement against miles per gallon with one point labelled, where the label is displaced a fixed distance from the point rather than sitting on top of it.
Figure 10.2: The offset argument pushing a label away from its point

10.14 Perpendicular Adjustment

The padj argument of the mtext() function fine-tunes the placement of the text in the direction perpendicular to adj. It takes values between 0 and 1. Compare the two plots below and observe the nudge:

10.15 Outer Margin

By default, mtext() places text on the inner margin of the current plot. Set outer = TRUE to place the text on the outer margin of the whole figure region instead. This is useful with multi-panel layouts, but first reserve outer space with par(oma = ...):

init <- par(no.readonly = TRUE)
par(oma = c(0, 0, 3, 0))
plot(mtcars$disp, mtcars$mpg)
mtext('Outer Margin Text', outer = TRUE)

par(init)

10.16 Exact Position

The at argument of the mtext() function places the text at an exact position along the margin, expressed in the user coordinates of the corresponding axis. Below, the text is anchored at disp = 200 on the bottom margin instead of the default center:

plot(mtcars$disp, mtcars$mpg)
mtext('Sample Text', side = 1, at = 200)
A scatterplot of displacement against miles per gallon with text added to the margin at explicitly chosen positions along the axes, rather than at the default margin locations.
Figure 10.3: The at argument placing margin text at specific coordinates