[R] I want axes that cross

Paul Johnson pauljohn32 at gmail.com
Mon Feb 16 07:02:07 CET 2009


On Fri, Feb 13, 2009 at 3:14 PM, Marc Schwartz
<marc_schwartz at comcast.net> wrote:
> on 02/13/2009 02:19 PM Paul Johnson wrote:
>> On Fri, Feb 13, 2009 at 1:51 PM, Marc Schwartz
>
> OK, so given all of the above, something like the following should work:
>
> set.seed(1233240)
> x <- rnorm(100)
> z <- gl(2,50)
> y <- rnorm(100, mean = 1.8 * as.numeric(z))
>
> # Calculate a new range, subtracting a definable value
> # from the min of each vector for the new minimum
> # Adust the 0.25 as may be needed
> X <- c(min(x) - 0.25, max(x))
> Y <- c(min(y) - 0.25, max(y))
>
> # Use 'X' and "Y' here, not 'x' and 'y'
> # So that the plot region is extended appropriately
> plot(X, Y, type = "n", axes = F, xlab = "x", ylab = "y")
>
> points(x, y, pch = "$", cex = 0.7, col = z)
>
> # DO use 'pos'...
> axis(1, pos = Y[1], col = "green", col.axis = "green")
> axis(2, pos = X[1], col = "red", col.axis = "red")
>
> # get the plot region boundaries
> usr <- par("usr")
>
> segments(X[1], usr[3], X[1], usr[4], col = "red")
> segments(usr[1], Y[1], usr[2], Y[1], col = "green")
>
>
> HTH,
>
> Marc
>
>

Thanks, Marc and everybody.

This last suggestion produces the graph I was trying for.

The other approaches that people suggest, using pos or xaxp, produce
other undesired changes in the tick marks or the position of the axes
relative to the data. xaxp offers the promise of a more intuitive
solution, except that, when using it, the tick marks are pushed off in
a bad way.  Your use of segments to draw the extensions of the axes
was the first intuition I had, but I did not know about the trick you
used to retrieve the size of the usr box.

More secrets of par, revealed.

Have you ever seen a drawing of the regions of an R plot with the
terminology that is used for parts?  Until I saw your example code, I
had not understood that the plot axes are placed at the absolute edge
of the user plotting region, for example.

-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas




More information about the R-help mailing list