[R] xyplot: distance between axis and axis-label gets wrong

Sebastian Weber sebastian.weber at physik.tu-darmstadt.de
Mon Jul 21 17:20:34 CEST 2008


Hi!

I just started reading the wonderful Lattice book and I finally found a
quite elegant solution for nicer log-ticks. However, there are some
problems with the spacing between the axis label and the axis tick
marks. It seems that lattice estimates the space wich gets used by the
labels before it calls the yscale.components function. However, this
function can change what is supposed to be plotted and therefore the
width can change making previous calculations void. Here is an example:

##
## functions for nice log-axis
##

logTicks <- function (lim, loc = c(1, 5), base=10)
{
  ii <-floor(log(range(lim), base)) + c(-1, 2)
  main <- base^(ii[1]:ii[2])
  r <- as.numeric(outer(loc, main, "*"))
  r[lim[1] <= r & r <= lim[2]]
}

xyscale.components.log <- function(lim, ..., side=c("bottom"), base=10,
majorTickFac=1.5, loc=c(1,5)) {
  if(side %in% c("left", "right"))
    ans <- yscale.components.default(lim = lim, ...)
  if(side %in% c("bottom", "top"))
    ans <- xscale.components.default(lim = lim, ...)

  tick.at <- logTicks(base^lim, loc = loc, base)
  tick.at.major <- logTicks(base^lim, loc = 1, base)
  major.powers <- log(tick.at.major, base)
  major.labels <- parse(text=paste(base, "^", major.powers, sep=""))
  major <- tick.at %in% tick.at.major
  ans[[side]]$ticks$at <- log(tick.at, 10)
  ans[[side]]$ticks$tck <- ifelse(major, majorTickFac, 1.0)
  ans[[side]]$labels$at <- log(tick.at, 10)
  ans[[side]]$labels$labels[major] <- major.labels
  ans[[side]]$labels$labels[!major] <- ""
  ans[[side]]$labels$check.overlap <- FALSE
  ans
}

xyscale.components.log.custom <- function(...) {
    args <- list(...)
    function(...) {
        dots <- list(...)
        do.call("xyscale.components.log", modifyList(dots, args))
    }
}


x <- 1:100
y <- x^3

xyplot(y ~ x, scales=list(log=T),
       xscale.component=xyscale.components.log.custom(side="bottom",
loc=c(1,3,8)),
       yscale.component=xyscale.components.log.custom(side="left",
loc=c(1,3,8)),
       ylab=expression(rho)
       )

xyplot(y ~ x, scales=list(log=T),
       ylab=expression(rho)
       )

In the first plot, the axis labels are too far away from the nicely
formatted 10^x expression. What can I do about it? A solution would be
to put in the labels via the scales-argument into the lattice-machinery,
but this is not very nice as I would have no automatic calculation of
the limits ...

Greetings,

Sebastian Weber



More information about the R-help mailing list