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

From: Sebastian Weber <sebastian.weber_at_physik.tu-darmstadt.de>
Date: Mon, 21 Jul 2008 17:20:34 +0200


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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 21 Jul 2008 - 15:52:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jul 2008 - 16:32:03 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive