[R] Secondary y axis

Austin, Matt maustin at amgen.com
Wed Oct 27 06:21:02 CEST 2004

You could replace your last two lines with the following.  You'll need to
play with the axes labeling to make it pretty.

scaled.c <- scale(c)
plot(scaled.c, ylim=c(-5, 5), xaxt="n", yaxt="n", col='green', type='l',
xlab="", ylab="")

axis (4,
      labels=round(seq(-2, 2, .25)*attr(scaled.c, 'scaled:scale')  +
attr(scaled.c, 'scaled:center'), 1),
      at=seq(-2, 2, .25), col="black", tck=-0.01, cex=.5)

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of John Robot
Sent: Tuesday, October 26, 2004 16:46 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Secondary y axis

I had a look at the help and previous discussions but I am
still unable to solve these issues:

I have a set of air pressure data. I would like to
plot a and b (both about 980-1000 millibar) and c, the difference
(around 0-8 millibar). I use axis (4) to create a new axis (to the right)
but I do not know how to assign c to the new axis which currently
features the same values as the first axis. So, how do I show a, b and c
in the same plot with different scales (one for each y-axis)?

I would like the x-axis to be time (h): 04, 05, 06 ... 24, 01, 02 ...
(the data is stored in the same table as a and b). How do define where
to get data for the a-axis and show for example every 5th value along
the axis? When I try plot (a ~ h ... the results are messed up in the

Is there a way to define the color within the box in the plot?

I pasted my current code here:
# Import

a = read.table("pres.txt", header=TRUE)
b = read.table("pres.txt", header=TRUE)
h = read.table("pres.txt", header=TRUE)

a = (a[,2])
b = (b[,3])
c = abs(a-b)
h = (h[,1])

f = seq(980, 995, 2.5)

### Plot

par (mfrow=c(1,1))    # rows, columns
par (cex.lab=1, col.axis="black", bg="lightyellow", fg="black", mar=c(4, 4, 
2.5, 2))    # cex sets font size

# Plot 1
plot (a, type="l", xlab="Time (h)", ylab="Temperature (°C)", ylim=c(980, 
995), pch=1, col="red", lwd=2, tck=-0.01)
abline (h=f, lwd=1, lty=2)

lines(b, pch=3, col="blue",  cex=2, type="l", lty=1, lwd=2) # lty controls 
line type
title("Air Pressure (mb)", font.main=1, adj=0.5, cex.main=2)

axis (4, labels=TRUE, tick=TRUE, col="black", tck=-0.01)
lines(c, pch=3, col="green", cex=2, type="l", lty=5, lwd=2)

Best regards,

Magnus Nilsson, Sweden

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list