[R] how to use axes=FALSE to get multple plots?

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 10 15:27:45 CET 2009


Dan Kelley wrote:
> I am trying to plot two things on one graph, with a y-axis at the left for
> one variable, and a y-axis at the right, for the other variable.  I thought
> I could do as follows
>
>
> par(mar=rep(4.5, 4))
> plot(0:10, 10:20)
> par(new=TRUE)
> plot(0:10, 100:110, axes=FALSE)
> axis(4)
>
>
> but this writes both y-axis labels on the left-hand side, and nothing on the
> right-hand side.  This is R 2.8.1 (2008-12-22) on OS-X.
>
> Am I missing something?  
>
> PS. I know that I can just make ylab="" and draw the right-hand axis name
> with mtext, but that then I would need to check par(mgp), to find where
> place it, etc.  I'm thinking that I am just missing something in my
> understanding.
>
>
>   
axis() doesn't draw axis labels.  There isn't a simple function for 
drawing text on side 4 as far as I know, but mtext is not so bad:  use

mtext("Right label", side=4, line=3)

to emulate the default ylab, but put it on the right.  See ?title for 
more details if you've changed the default locations.

I don't like to use plot(new=TRUE), because there are lots of things 
that can go wrong when you overplot high level graphics (I'd use 
points() instead), but your example is one of the few cases where you 
can make just as many errors using points() as using par(new=TRUE).  Are 
you really sure you want a graph with two different scales on it?

Duncan Murdoch




More information about the R-help mailing list