3

Say I want to create an ordinary xyplot without explicitly specifying axis limits, then how are axis limits calculated?

The following line of code produces a simple scatter plot. However, axis limits do not exactly range from 1 to 10, but are slightly expanded to the left and right and top and bottom sides (roughly by 0.5).

library(lattice)
xyplot(1:10 ~ 1:10, cex = 1.5, pch = 20, col = "black", 
       xlab = "x", ylab = "y")

xyplot_axis_extension

Is there any way to determine the factor by which the axes were expanded on each site, e.g. using trellis.par.get? I already tried the following after executing the above-mentioned xyplot command:

library(grid)
downViewport(trellis.vpname(name = "figure"))
current.panel.limits()
$xlim
[1] 0 1

$ylim
[1] 0 1

Unfortunately, the panel limits are returned as normalized parent coordinates, which makes it impossible to obtain the "real" limits. Any suggestions would be highly appreciated!

Update:
Using base-R plot, the data range (and consequently the axis limits) is by default extended by 4% on each side, see ?par. But this factor doesn't seem to apply to 'trellis' objects. So what I am looking for is an analogue to the 'xaxs' (and 'yaxs') argument implemented in par.

fdetsch
  • 4,484
  • 3
  • 26
  • 49
  • Do you want to know how to change it to something else (a different %), or the internal working of lattice on what's the actual arithmetic it uses by default? – Gimelist Mar 12 '15 at 11:57
  • The latter. By the way, I found out that 'trellis' axis limits do agree with the above-mentioned 4% (at least for the code I am using). But where is this expansion factor defined? – fdetsch Mar 13 '15 at 07:28
  • I tried looking into lattice's source code, but no luck. I'm pretty sure it's there somewhere, just didn't have enough time to actually find it. – Gimelist Mar 15 '15 at 03:00

1 Answers1

4

Axis limits for xyplot are calculated in the extend.limits function. This function isn't exported from the lattice package, so to see it, type lattice:::extend.limits. Concerning a numeric vector, this function is passed the range of values from the corresponding data (c(1, 10) in this example). The final limits are calculated according to the following equation:

lim + prop * d * c(-1, 1)
  • lim are the limits of the data, in this case c(1, 10)
  • prop is lattice.getOption("axis.padding")$numeric, which by default is 0.07
  • d is diff(as.numeric(lim)), in this case 9

The result in this case is c(0.37, 10.63)

In case you're interested, the call stack from xyplot to extend.limits is

  1. xyplot
  2. xyplot.formula
  3. limits.and.aspect
  4. limitsFromLimitList
  5. extend.limits
BenBarnes
  • 17,996
  • 6
  • 53
  • 70