[R] Double labels and scales

Duncan Murdoch murdoch at stats.uwo.ca
Fri Feb 2 14:48:39 CET 2007


On 2/2/2007 8:07 AM, Lorenzo Isella wrote:
 > Dear All,
 > Say you want to plot, on the same figure two quantities, concentration
 > and temperature, both as function of the same variable.
 > I'd like to be able to put a certain label and scale on the y axis on
 > the left of the figure (referring to the temperature) and another
 > label and scale for the concentration on the right.
 > Any suggestion about what to do?
 > I am sure it is trivial, but I could not find what I needed on the
 > net. I found some reference about a plot.double function by Anne York,
 > but I do not think that I need anything so elaborate.
 > Many thanks

One way is to work out the linear transformation that puts the other one 
in the scale of the one you want on the left axis, then plot both and 
manually apply an axis to the right side.

For example:

x <- 1:10
temp <- rnorm(10, mean=5-20*x)
conc <- rnorm(10, mean=x)

b <- diff(range(temp))/diff(range(conc))
a <- min(temp) - b*min(conc)

par(mar=c(5,5,5,5))

plot(x, temp, ylim = range(c(temp, a + b*conc)), type='l')
lines(x, a + b*conc, lty=2)

ticks <- pretty(conc)
axis(4, at=a + b*ticks, labels=ticks)
mtext("conc", 4, 3)
legend("top", c("temp", "conc"), lty=1:2)



More information about the R-help mailing list