[R] lattice logaritmic scale (basis "e" ), rewriting labels using xscale.component

Katharina May may.katharina at googlemail.com
Thu Jun 18 12:56:29 CEST 2009


Hi there,

sorry for troubling everybody once again, I've got a problem rewriting
Sarkar's function for
rewriting the tick locations in a logaritmic way (s.
http://lmdvr.r-forge.r-project.org/code/Chapter08.R):

His example works for log 2 but I need log e (natural logarithm). My
problem is that if I replace
2 with "e" (using paste()), I get the error message that the location
isn't a numeric value.

Is there any way to get this working somehow or do I have to take a
different approach?

Thanks, Katharina

Here my failing approach:

require(lattice)
data(Earthquake, package = "MEMSS")

xscale.components.log <- function(lim, ...) {
    ans <- xscale.components.default(lim = lim, ...)
    tick.at <- logTicks(paste("e^",lim,sep=""), loc = c(1, 3))
    ans$bottom$ticks$at <- log(tick.at, 2)
    ans$bottom$labels$at <- log(tick.at, 2)
    ans$bottom$labels$labels <- as.character(tick.at)
    ans
}

logTicks <- function (lim, loc = c(1, 5)) {
    ii <- floor(log(range(lim))) + c(-1, 2)
    main <- paste("e^",(ii[1]:ii[2]),sep="")
    r <- as.numeric(outer(loc, main, "*"))
    r[lim[1] <= r & r <= lim[2]]
}
xyplot(accel ~ distance, data=Earthquake, scales = list(log = "e"),
xscale.components = xscale.components.log,




Here is the original  code of Sarkar:

logTicks <- function (lim, loc = c(1, 5)) {
    ii <- floor(log10(range(lim))) + c(-1, 2)
    main <- 10^(ii[1]:ii[2])
    r <- as.numeric(outer(loc, main, "*"))
    r[lim[1] <= r & r <= lim[2]]
}
xscale.components.log2 <- function(lim, ...) {
    ans <- xscale.components.default(lim = lim, ...)
    tick.at <- logTicks(2^lim, loc = c(1, 3))
    ans$bottom$ticks$at <- log(tick.at, 2)
    ans$bottom$labels$at <- log(tick.at, 2)
    ans$bottom$labels$labels <- as.character(tick.at)
    ans
}




More information about the R-help mailing list