[R] Secondary Y axis title

Dave Hewitt dhewitt at vims.edu
Sun Oct 21 00:18:15 CEST 2007


The controls for the margin sizes, which set the amount of space allocated 
for the labels, are the 'mai' numbers in the first 'par' call. The fourth 
number changed to 0.9 or greater should give you more room like you're 
looking for.

You can use the alternative 'mtext' way of adding the label (it's simpler), 
which I have used below to make the right label appear the same distance 
from the axis as the left label (option "line = 3"). I don't see how to 
adjust the reading direction of the label to be top-to-bottom with 'mtext', 
so I guess you'll need the 'text' way of doing it if you want that 
orientation (although I don't understand the 'usr' positions that define 
the location).

Gabor, do you know how to change the reading direction with 'mtext'? I 
tried 'srt' and that did not work.


library(zoo)

set.seed(1)
z <- zoo(cbind(A = cumsum(rnorm(100)), B = cumsum(rnorm(100, mean = 0.2))))

par(mai = c(.8, .8, .2, .9))  # Or change '.9' to a larger number for even 
more room (the units are inches)

plot(z[,1], type = "l", xlab = "X axis label", ylab = colnames(z)[1], lty = 
1, lwd = 2)

par(new = TRUE)

plot(z[,2], type = "l", ann = FALSE, yaxt = "n", col = "blue", lty = 2, lwd 
= 2)

axis(4)

legend(x = "topleft",  bty = "n",  lty = c(1,2),  lwd = c(2,2),
        col = c("black", "blue"),  legend = paste(colnames(z), c("(left 
y-axis)", "(right y-axis)")))

mtext(colnames(z)[2], 4, line = 3, col = "blue")

--Dave

At 11:42 PM 10/20/2007 +0200, John Theal wrote:
>I still haven't resolved this problem.  However, when I run the plot.zoo 
>example the label appears, it also appears if I enter the commands 
>manually.  The problem that persists is that the label has to lie so close 
>to the secondary y axis, that (in my case) it becomes almost 
>indistinguishable from the axis scale labels.  If the label is long, then 
>it over-writes some of the scale labeling.  I thought it might be do to 
>the way the Quartz window manager handles the figure margins (under Mac OS 
>X), but it looks as though maybe this is not the case.
>
>I'm running the same versions of R (2.6.0, unpatched) and zoo (1.4-0) as 
>Gabor.  I am using R under Mac OS X.
>Dave Hewitt wrote:
>>I was curious about the exact same question that John Theal posed - how 
>>to get a second y-axis label for a plot of two data series against a 
>>common x (in my case, time).
>>
>>I tried two different methods - one in lattice and one in plot. Both 
>>times the y-axis on the right side appears but the label does not. 
>>Similar to John's comment, it appears that there is not enough room for 
>>the label.
>>
>>Gabor mentioned that the plot.zoo examples give a plot like this. When I 
>>run that series of examples, #7 appears to be what Gabor was referring 
>>to, but even there the label for the right-hand y-axis is missing.
>>
>>Help?
>>
>>Dave Hewitt



More information about the R-help mailing list