I’ve been asked how to do this several times, so I thought it might help to put an example online.
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))


#1 by Logan on 4 November 2009 - 10:04 am
Quote
Thank you very much for posting this code. It was exactly what I needed.
#2 by Duleep Kumar Samuel on 8 January 2010 - 4:07 pm
Quote
Thank you so much, lovely graph
#3 by Julius Sumihar on 12 August 2010 - 8:28 pm
Quote
Hey, thanks a lot for making this example online. It has helped me! Thanks!!