[R] Second axis in a plot

Petr Pikal petr.pikal at precheza.cz
Tue Jun 18 14:27:04 CEST 2002

```
On 17 Jun 2002 at 15:16, Juan Cuervo wrote:

> Hi to all,
> First of all, I prefer to tell that I am a R-newbie,
> so I apologize if this is a silly question (I have
> tried looking in the manuals, but without luck).
> I have two variables, y and z, that I want to plot
> against x in the same plot. I have done this before,
> using points() after plot(). But now the problem is
> that y and z are in different units of measurement,
> and their ranges are very different.
> I have tried drawing a second axis with axis(), but I
> found the same problem: the new axis is drawn in the
> same range than the first variable plotted in the
> y-axis.
> I would greatly appreciate some suggestions.
> Regards,
> Juan L. Cuervo
>

Try this small function I made for myself (therefore it is not very well described).

yylab is for right and left hand side axes labels
znac is for pch
bar is for col
smooth is for smoothing

actually it is very similar to prof.Ripley's answer.

#----------------------------------------------------------------------------------------------
--------
# graf na 2 osach y

smooth=0, ...)

{

par(mar=c(5,4,4,2),oma=c(0,0,0,3))
plot(x,yright,axes=F,ylab="",pch=znac[1],col=bar[1], ...)
axis(4,pretty(range(yright),10))

if (smooth!=0) lines(supsmu(x,yright,span=smooth),col=bar[1], ...)

if(yylab[1]=="")
mtext(deparse(substitute(yright)),side=4,outer=T,line=1, col=bar[1])
else
mtext(yylab[1],side=4,outer=T,line=1, col=bar[1])

par(new=T)
plot(x,yleft,ylab="",pch=znac[2],col=bar[2], ...)

if(yylab[2]=="")
mtext(deparse(substitute(yleft)),side=2,line=2, col=bar[2])
else
mtext(yylab[2],side=2,line=2, col=bar[2])

if (smooth!=0) lines(supsmu(x,yleft,span=smooth),col=bar[2], lty=2, ...)

}

>
> _______________________________________________________________
> Copa del Mundo de la FIFA 2002
> El único lugar de Internet con vídeos de los 64 partidos.
> ¡Apúntante ya! en http://fifaworldcup.yahoo.com/fc/es/
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.- r-help mailing list -- Read
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help",
> or "[un]subscribe" (in the "body", not the subject !)  To:
> r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._

Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```