Skip to content
Prev 170468 / 398506 Next

I want axes that cross

on 02/13/2009 02:19 PM Paul Johnson wrote:
'bty' is referenced in ?box as a '...' argument, though the detailed
options are defined in ?par.
OK...so if I am reading this more clearly, you don't want them to simply
join in the corner, but to actually cross for some length?
Yes, strictly speaking it is the plot region's dimensions that are
extended by 4%, when these two parameters are set to 'r', which is the
default.

However, the actual drawn axes *may* be affected by this, since the data
range has been extended and this will affect the default calculation of
the axis tick marks, which is dependent upon the resultant range.
...
Style "r" (regular) first extends the data range by 4 percent at each
end and then finds an axis with pretty labels that fits within the
extended range. Style "i" (internal) just finds an axis with pretty
labels that fits within the original data range.
...


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