8.8 Changing the Text of Tick Labels

8.8.1 Problem

You want to change the text of tick labels.

8.8.2 Solution

Consider the scatter plot in Figure 8.15, where height is reported in inches:

To set arbitrary labels, as in Figure 8.15 (right), pass values to breaks and labels in the scale. One of the labels has a newline (\n) character, which tells ggplot to put a line break there:

Scatter plot with automatic tick labels (left); With manually specified labels on the y-axis (right)Scatter plot with automatic tick labels (left); With manually specified labels on the y-axis (right)

Figure 8.15: Scatter plot with automatic tick labels (left); With manually specified labels on the y-axis (right)

8.8.3 Discussion

Instead of setting completely arbitrary labels, it is more common to have your data stored in one format, while wanting the labels to be displayed in another. We might, for example, want heights to be displayed in feet and inches (like 5’6“) instead of just inches. To do this, we can define a formatter function, which takes in a value and returns the corresponding string. For example, this function will convert inches to feet and inches:

Here’s what it returns for values 56–64 (the backslashes are there as escape characters, to distinguish the quotes in a string from the quotes that delimit a string):

Now we can pass our function to the scale, using the labels parameter (Figure 8.16, left):

Here, the automatic tick marks were placed every five inches, but that looks a little off for this data. We can instead have ggplot set tick marks every four inches, by specifying breaks (Figure 8.16, right):

Scatter plot with a formatter function (left); With manually specified breaks on the y-axis (right)Scatter plot with a formatter function (left); With manually specified breaks on the y-axis (right)

Figure 8.16: Scatter plot with a formatter function (left); With manually specified breaks on the y-axis (right)

Another common task is to convert time measurements to HH:MM:SS format, or something similar. This function will take numeric minutes and convert them to this format, rounding to the nearest second (it can be customized for your particular needs):

Running it on some sample numbers yields:

The scales package, which is installed with ggplot2, comes with some built-in formatting functions:

  • comma() adds commasto numbers, in the thousand, million, billion, etc. places.
  • dollar() adds a dollar sign and rounds to the nearest cent.
  • percent() multiplies by 100, rounds to the nearest integer, and adds a percent sign.
  • scientific() gives numbers in scientific notation, like 3.30e+05, for large and small numbers.

If you want to use these functions, you must first load the scales package, with library(scales).