Skip to content

Two y-axis in plots

5 messages · Allan McRae, Jerome Asselin, Ott Toomet +2 more

#
See the axis() function.

x <- rnorm(20)
y<- rnorm(20)
plot(x,y)
axis(4,at=-3:3,labels=(-3:3)*10)
axis(3,at=-3:3,labels=letters[1:7])

You may want to also consider plot(x,y,xaxt="n",yaxt="n") to suppress the 
x and y axes which you can rebuild with axis() to fit your purpose. See 
also the help file on par() for all kinds graphicals arguments. E.g., you 
may have to reset your margins with the "mai" options if you need enough 
space to include a label on the right axis.

Jerome
On April 3, 2003 02:53 am, Allan McRae wrote:
#
Hi,

 | From: "Allan McRae" <s0238397 at sms.ed.ac.uk>
 | Date: Thu, 3 Apr 2003 11:53:32 +0100
 | 
 | Hi,
 | 
 | I am trying to plot two data sets on one plot but with
 | using a different y-axis ranges for each - preferably with one shown on each side of the graph.

This is a common question, you may try to search the mail archives.
There is no ready-made function in any of the common packages.
There may be some more-or-less suitable function either in the mailing
list or somewhere in the less common packages.

Basically what you have to do is to rescale your data, plot on the
same graph and draw a new axis using axis().
Perhaps it helps.

Ott
#
Allan  -

Nothing canned, as far as I know.  For a single plot,
use  plot(),  then  points(), axis(4, ...).  See the
help pages for each one.  Wehn plotting the second
data set using points, you will need to shift and
scale the vertical coordinate yourself, to the scale
of the first dataset, and do the same for axis().

Here's a somewhat cooked example.

data.x <- c(1:50)
data.y <- exp(rnorm(50))
data.p <- rank(data.y) / 50  #  percentiles

plot(data.x, data.y, pch="+")
temp <- range(data.y)
points(data.x,    temp[1] + (temp[2] - temp[1]) * data.p, pch=20)
axis(side=4, at = temp[1] + (temp[2] - temp[1]) * c(0:5) / 5, labels = c(0:5) / 5)

What's cooked here is that I know by construction that
data.p lies in 0 to 1, therefore I don't have
to do a more complicated shift and scaling to get the
second data set into the vertical scale of the first.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Thu, 3 Apr 2003, Allan McRae wrote:

            
#
Have you considered "plot" with "axes" and "points" or "lines"?

"?plot" provides examples using "points" and "lines";  "?axes" should 
fill in the gap.

Spencer Graves
Allan McRae wrote: