# [R] Different y-axes

Petr Pikal petr.pikal at precheza.cz
Wed Jun 12 08:59:20 CEST 2002

```
On 11 Jun 2002 at 12:40, Gregor Gawron wrote:

> Hi All,
>
> I have checked everything I could find abot graphics, but still cannot
> solve the problem. Are there any ways to make a graph that plots two
> lines and two different y-axes, each of them has a scale that is
> related to the respective line. For example, y1 has a range 1:50 and
> y1 ranges 0:1. The x-axe is the same for both.
>
> Thank you in advance.

I do not know if it is a pure coincidence, but I recently tried to
make similar plot. As you did not get any strightforward answer, I
decided to write down a little function, which can make such plot.

I am sure it can be improved by more experienced programmers
but it seems to work as I (and maybe also you:-) wish.

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

plot.yy<-function(x,yright,yleft,
yylab=c("",""),znac=c(1,2),bar=c(1,2), linky=F, ...)

{

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], ...)

if (linky) lines(x,yright,col=bar[1], ...)

axis(4,pretty(range(yright),10))

if(yylab[1]=="")
mtext(deparse(substitute(yright)),side=4,outer=T,line=1)
else
mtext(yylab[1],side=4,outer=T,line=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)
else
mtext(yylab[2],side=2,line=2)

if (linky) lines(x,yleft,col=bar[2], lty=2, ...)

}

*znac* is coding vector for pch, *bar* for col, *yylab* is
character vector for axes labels and *linky* is for drawing lines.

It gives you some warnings sometimes but it still works.

Hope it helps.

>
> ---
> Gregor Gawron
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.- 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```

More information about the R-help mailing list