Labeling the Vertical Axis in R Plots

December 24, 2012
By

(This article was first published on Carlisle Rainey » R, and kindly contributed to R-bloggers)

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.

n = 100
x = rnorm(n)
y = rnorm(n, x)


plot(x, y, axes = F,
xlab = NA,
ylab = NA)
abline(lm(y~x), col = "red", lwd = 2)
box()
axis(side = 1, tck = -.015, labels = NA)
axis(side = 2, tck = -.015, labels = NA)
axis(side = 1, lwd = 0, line = -.4)
axis(side = 2, lwd = 0, line = -.4, las = 1)
mtext(side = 1, "Explanatory Variable", line = 2)
mtext(side = 2, "Outcome Variable", line = 2)
mtext(side = 3, "A Nice Looking Scatterplot")

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.

loc = par("usr")

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.

text(loc[1], loc[4], "Outcome\nVariable", pos = 3, xpd = T)

(Notes: The '\n' in the label starts a new line. The 'xpd=T' 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 0 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.


n = 100
x = rnorm(n)
y = rnorm(n, x)


plot(x, y, axes = F,
xlab = NA,
ylab = NA)
abline(lm(y~x), col = "red", lwd = 2)
box()
axis(side = 1, tck = -.015, labels = NA)
axis(side = 2, tck = -.015, labels = NA)
axis(side = 1, lwd = 0, line = -.4)
axis(side = 2, lwd = 0, line = -.4, las = 1)
mtext(side = 1, "Explanatory Variable", line = 2)
mtext(side = 3, "A Nice Looking Scatterplot", line = 2, font = 2)
loc text(loc[1], loc[4], "Outcome\nVariable", xpd = T, adj = c(1,0))

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.


I encourage you to share this with others and contribute to the conversation at Labeling the Vertical Axis in R Plots, which first appeared at carlislerainey.com. For more of my thoughts and ideas, subscribe to my blog and follow me on Twitter. You also might like to browse my archive and read my papers on Strategic Mobilization and Testing Hypotheses of No Meaningful Effect.
Follow @carlislerainey

To leave a comment for the author, please follow the link and comment on his blog: Carlisle Rainey » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.