# [R] Second axis in a plot

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Mon Jun 17 16:10:39 CEST 2002

```Here is an easier way

x <- 1:10
y <- rnorm(10)
z <- runif(10, 1000, 10000)
par(mar=c(5,4,4,4) + 0.1)  # Leave space for z axis
plot(x, y)
par(new=T)
plot(x, z, type="l", axes=F, bty="n", xlab="", ylab="")
axis(4, at=pretty(range(z)))
mtext(z, 4, 3)

On 17 Jun 2002, Peter Dalgaard BSA wrote:

> Juan Cuervo <juan_l_cuervo at yahoo.es> writes:
>
> > 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
>
> The trick is to rescale z into y coordinates, and then axis(3, at=...,
> labels=...) where "at" is in y coordinates, but the labels are given
> as z coordinates.
>
> --
>    O__  ---- Peter Dalgaard             Blegdamsvej 3
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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

```