[R] plot with two y axes
Jim Lemon
bitwrit at ozemail.com.au
Mon Sep 11 12:36:48 CEST 2000
Thanks to Uwe Ligges, who found a couple of bugs in the function I
posted last week. Here's a (hopefully) debugged version.
Jim
-------------- next part --------------
plot2ax<-function(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab,type="b",pch1=1,pch2=2,...) {
if(!missing(y1)) {
oldmar<-par("mar")
par(mar=c(5,4,4,4))
if(missing(x1)) x1<-1:length(y1)
if(missing(xlim1)) xlim1<-range(x1)
if(missing(ylim1)) ylim1<-range(y1)
plot(x1,y1,xlim=xlim1,ylim=ylim1,pch=pch1,type=type,...)
if(!missing(y2)) {
if(missing(x2)) x2<-1:length(y2)
if(missing(xlim2)) xlim2<-range(x2)
if(missing(ylim2)) ylim2<-range(pretty(y2))
ax2val<-pretty(y2)
xmult<-(xlim1[2] - xlim1[1])/(xlim2[2] - xlim2[1])
ymult<-(ylim1[2] - ylim1[1])/(ylim2[2] - ylim2[1])
axis(4,(ax2val-ylim2[1])*ymult+ylim1[1],labels=as.character(ax2val))
points((x2-xlim2[1])*xmult+xlim1[1],(y2-ylim2[1])*ymult+ylim1[1],
pch=pch2,type=type)
if(!missing(y2lab)) mtext(y2lab,4,2)
}
par(oldmar)
}
else cat("Usage: plot2ax(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab,type=\"b\",pch1=1,pch2=2)\n")
}
More information about the R-help
mailing list