Example R code for graph with two y axes


Example code to produce a plot with two y axes using R. R is available free from the R CRAN repository.

#  Author:   James Goode
#            Tendron Systems Ltd
#  Example plot with two y axes
n = 150      # number of points
y1 = cumsum(rnorm(n))
y2 = cumsum(rnorm(n, mean=0.5))

par(mai=c(.9, .8, .4, .8))   # margin size, in inches, c(bottom, left, top, right)
plot(1:n, y1, type="l", col="blue", xlab="X values", main="Plot with two axes")
plot(1:n, y2, type="l", ann=FALSE, yaxt="n")
legend(x="topleft", bty="y", lty=c(1,1), col=c("blue","red"),
legend=c("Line 1 (left y-axis)", "Line 2 (right y-axis)"))