[R] I want axes that cross

Marc Schwartz marc_schwartz at comcast.net
Fri Feb 13 20:51:12 CET 2009


on 02/13/2009 01:25 PM Paul Johnson wrote:
> Hello, everybody.
> 
> A student asked me a howto question I can't answer.  We want the
> length of the drawn axes to fill the full width and height of the
> plot, like so:
> 
>    |
>    |         *
>    |           *
>    |     *
> ---|-----------------
> 
> However, when we use plot with axes=F and then use the axis commands
> to add the axes, they do not cross over each other. We get
> 
> 
>    |         *
>    |           *
>          *
>        ----------
> 
> The axes are not wide enough to cover the entire range of the data. We
> do not want to add a box() command, which is a usual answer for this,
> because we don't want to draw on top or the side.
> 
> Here's my test case:
> 
> x <- rnorm(100)
> z <- gl(2,50)
> y <- rnorm(100, mean= 1.8*as.numeric(z))
> 
> plot(x,y,type="n", axes=F)
> points(x,y, pch="$",cex=0.7, col=z)
> axis(1, col="green", col.axis="green")
> axis(2, col="red", col.axis="red")
> 
> Can I cause the 2 axes to "cross" as desired?
> 
> The axis help says the axis is clipped at the plot region, but it
> seems to get clipped even more narrowly thanthat.
> 
> I've been searching in r-help quite a bit and am a little surprised
> how difficult this is....

Paul,

I am guessing that you want:

x <- rnorm(100)
z <- gl(2,50)
y <- rnorm(100, mean= 1.8*as.numeric(z))

plot(x,y,type="n", axes=F)
points(x,y, pch="$",cex=0.7, col=z)
axis(1, col="green", col.axis="green")
axis(2, col="red", col.axis="red")

# Draw the box like an "L" on the bottom and left only
box(bty = "l")


Note that you can specify which sides the 'box' is created upon by using
the 'bty' argument. See ?box for more information.

Also, by default, the axes extend the range of 'x' and 'y'  by 4%. You
can use 'xaxs = i' and 'yaxs = i' in the plot() call to restrict the
axes to the true ranges of 'x' and 'y'.  This would be important, for
example, when you want the lower left hand corner of the plot to be at
exact coordinates such as 0,0.

See ?par for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list