8.14 Using a Logarithmic Axis

8.14.1 Problem

You want to use a logarithmic axis for a graph.

8.14.3 Discussion

With a log axis, a given visual distance represents a constant proportional change; for example, each centimeter on the y-axis might represent a multiplication of the quantity by 10. In contrast, with a linear axis, a given visual distance represents a constant quantity change; each centimeter might represent adding 10 to the quantity.

Some data sets are exponentially distributed on the x-axis, and others on the y-axis (or both). For example, the Animals data set from the MASS package contains data on the average brain mass (in g) and body mass (in kg) of various mammals, with a few dinosaurs thrown in for comparison:

As shown in Figure 8.26, we can make a scatter plot to visualize the relationship between brain and body mass. With the default linearly scaled axes, it’s hard to make much sense of this graph. Because of a few very large animals, the rest of the animals get squished into the lower-left corner-a mouse barely looks different from a triceratops! This is a case where the data is distributed exponentially on both axes.

ggplot will try to make good decisions about where to place the tick marks, but if you don’t like them, you can change them by specifying breaks and, optionally, labels. In the example here, the automatically generated tick marks are spaced farther apart than is ideal. For the y-axis tick marks, we can get a vector of every power of 10 from 100 to 103 like this:

The x-axis tick marks work the same way, but because the range is large, R decides to format the output with scientific notation:

And then we can use those values as the breaks, as in Figure 8.27 (left):

To instead use exponential notation for the break labels (Figure 8.27, right), use the trans_format() function, from the scales package:

Scatter plot with log~10~ x- and y-axes, and with manually specified breaks (left); With exponents for the tick labels (right)Scatter plot with log~10~ x- and y-axes, and with manually specified breaks (left); With exponents for the tick labels (right)

Figure 8.27: Scatter plot with log10 x- and y-axes, and with manually specified breaks (left); With exponents for the tick labels (right)

Another way to use log axes is to transform the data before mapping it to the x and y coordinates (Figure 8.28). Technically, the axes are still linear – it’s the quantity that is log-transformed:

Plot with log transform before mapping to x- and y-axes

Figure 8.28: Plot with log transform before mapping to x- and y-axes

The previous examples used a log10 transformation, but it is possible to use other transformations, such as log2 and natural log, as shown in Figure 8.29. It’s a bit more complicated to use these – scale_x_log10() is shorthand, but for these other log scales, we need to spell them out:

Plot with exponents in tick labels. Notice that different bases are used for the x and y axes.

Figure 8.29: Plot with exponents in tick labels. Notice that different bases are used for the x and y axes.

It’s possible to use a log axis for just one axis. It is often useful to represent financial data this way, because it better represents proportional change. Figure 8.30 shows Apple’s stock price with linear and log y-axes. The default tick marks might not be spaced well for your graph; they can be set with the breaks in the scale:

Top: a stock chart with a linear x-axis and log y-axis; bottom: with manual breaksTop: a stock chart with a linear x-axis and log y-axis; bottom: with manual breaks

Figure 8.30: Top: a stock chart with a linear x-axis and log y-axis; bottom: with manual breaks