[R] Plotting w/multiple y-axes?

Per Bak perbak at brunet.bn
Mon Jan 20 15:03:02 CET 2003


Hi Petr,

Thank you for taking the time to answer! 
I've deciphered your advanced function and cut it down to the bare bones. I 
did not know about the par(new=T) command and it also took me a while to sort 
out the axes. I'll take it from there.

Kind regards,

Per Bak,
Brunei

x <- 0:10
y1 <- x
y2 <- x^2

plot(x, y1, axes=F, ylab="", type="b")
axis(2, pretty(range(y1),10))

par(new=T)

plot(x, y2, axes=F, ylab="", type="b")
axis(4, pretty(range(y2),10))

axis(1,pretty(range(x),10))


On Monday 20 January 2003 12:46, Petr Pikal wrote:
> Hi
>
> On 20 Jan 2003 at 18:51, Per Bak wrote:
> > How do I plot using multiple(2) y-axes?
> > I have two series that use the same x-data, but have very different
> > scales.
>
> This is my function for plotting 2 y axes on the same graph. (not very well
> documented, but not so complicated either :-) Sometimes it gives you a
> warning.
> Requires either library chron (plotting dates on x axis) or  xDatum = F.
>
> plot.yy<-function(x,yright,yleft, xDatum=T, xlab = NULL, yylab=c("",""),
> pch=c(1,2), col=c(1,2), linky=F, smooth=0, lwds=1, ...)
>
> {
>
> par(mar=c(5,4,4,2),oma=c(0,0,0,3))
> plot(x,yright,axes=F,ylab="", xlab=xlab, pch=pch[1],col=col[1], ...)
> axis(4,pretty(range(yright,na.rm=T),10),col=col[1])
>
> if (linky) lines(x,yright,col=col[1], ...)
>
> if (smooth!=0) lines(supsmu(x,yright,span=smooth),col=col[1], lwd=lwds,
> ...)
>
> if(yylab[1]=="")
> mtext(deparse(substitute(yright)),side=4,outer=T,line=1, col=col[1], ...)
> else
> mtext(yylab[1],side=4,outer=T,line=1, col=col[1], ...)
>
> par(new=T)
> plot(x,yleft,ylab="", axes=F ,xlab=xlab, pch=pch[2],col=col[2], ...)
> box()
> axis(2,pretty(range(yleft,na.rm=T),10),col=col[2])
> if (xDatum) axis(1,dates(pretty(range(datum,na.rm=T),10)),
> labels=as.character(chron(pretty(range(datum,na.rm=T),10)),
> format=c("d/m/y"))) else axis(1,pretty(range(x,na.rm=T),10))
>
> if(yylab[2]=="")
> mtext(deparse(substitute(yleft)),side=2,line=2, col=col[2], ...)
> else
> mtext(yylab[2],side=2,line=2, col=col[2], ...)
>
>
> if (linky) lines(x,yleft,col=col[2], lty=2, ...)
> if (smooth!=0) lines(supsmu(x,yleft,span=smooth),col=col[2], lty=2,
> lwd=lwds, ...)
>
> }
>
> > Appreciate any feedback,
> >
> > Per Bak
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> CheersPetr Pikal
> Precheza a.s., Nabx.Dr.E.BeneÜe 24, 750 62 Pxerov
> tel: +420581 252 257 ; 724 008 364
> petr.pikal at precheza.cz; p.pik at volny.cz
> fax +420581 252 561




More information about the R-help mailing list