Labeling the Vertical Axis in R Plots

Posted on December 24th, 2012

In response to a recent post (Getting Control of Axes in R Plots), a reader suggests labeling the vertical axis slightly different than normal. Rather than label the axis with vertical text positioned outside the plot area and centered along the axis (as I usually do), Kate suggests placing the label at the top of the axis. In this post, I discuss how to orient the label horizontally at the top of the axis.

Kate points toward Stephen Few's (perceptualedge.com , books) designs as a template and suggests this example in particular.

Design Example Solution

Other excellent examples can be found in his report "Sometimes We Must Raise Our Voices" (pdf).

To start, let's rebuild the graph I created in a previous post, using the code below.

This code creates the plot below.

prev

We need to make the following changes:

  1. Drop the current vertical axis label. Do this by deleting the second call to the mtext()  function.
  2. Make the title bold and move it a bit higher. Since we are moving the vertical axis label to the top of the plot, we need to make the title stand out a bit more. Do this by adding line = 1  and font = 2  argument to the third mtext()  call. This moves the title up and bolds the text, respectively.
  3. Finally, we need to move the vertical axis label. I discuss the process below.

First, it is convenient to find and save the corners of the plotting region. This allows us to easily place the text in relation to the upper-left corner of the box. This can be done with the following code.

The first and fourth element of this vector form the x-y coordinates of the upper-corner.

It is easy to simply add text above the axis using the code below.

(Notes: The \n  in the label starts a new line. The xpd=TRUE  argument allows writing text outside the plotting region.)

This creates the plot below.

first_try

This is a little more like what Kate wants, but not quite. Centering the text above the axis looks okay, but it might look better if the text were above and right-aligned against the axis. Unfortunately, this requires a bit of trial and error. We can easily right-align the label by changing the position argument from pos = 3  to pos = 2 . However, this does not position the text above the corner point. To do this, we simply need to delete the pos  argument and replace it with an adj  argument. The adj  argument takes a vector of length two whose elements lie in the set [0,1].  The R help file explains it better than I can.

The value of  adj determines the way in which text strings are justified in  textmtext and  title. A value of   produces left-justified text,  0.5 (the default) centered text and  1 right-justified text. (Any value in [0, 1] is allowed, and on most devices values outside that interval will also work.)

Note that the  adj argument of  text also allows  adj = c(x, y) for different adjustment in x- and y- directions. Note that whereas for  text it refers to positioning of text about a point, for  mtext and  title it controls placement within the plot or device region.

Based on this, we simply need need to add an adj = c(1,0)  argument (i.e. align the right- and bottom-side of the text against the points). (Note: the  pos argument will override the adj  argument, so make sure that it is deleted.) This gives us what we want. The new code is given below.

This code produces the following plot, which I think is close to what Kate wants.

final

Perhaps this plot can be improved a bit by moving the label around just a little more. Do this by adding small numbers to 'loc[1]' and 'loc[4]' . However, this exercise is left to the reader.