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")
par(new=TRUE)
plot(1:n, y2, type="l", ann=FALSE, yaxt="n")
axis(4)
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)"))


demo2axisPlot

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s