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

Katharina May may.katharina at googlemail.com
Tue Jun 23 00:22:40 CEST 2009


thanks Deepayan, that works great!


2009/6/19 Deepayan Sarkar <deepayan.sarkar at gmail.com>:
> On 6/18/09, Katharina May <may.katharina at googlemail.com> wrote:
>> 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.
>
> R doesn't have a constant that represents e, but
>
>  tick.at <- logTicks(exp(lim), loc = c(1, 3))
>
> instead of
>
>  tick.at <- logTicks(paste("e^",lim,sep=""), loc = c(1, 3))
>
> should give you e^lim. Or, if it makes it easier,
>
>  e <- exp(1)
>  tick.at <- logTicks(e^lim, loc = c(1, 3))
>
> I don't think you need to change the logTicks function.
>
> -Deepayan
>
>>  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
>>  }
>>
>>  ______________________________________________
>>  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.
>>
>



-- 
Time flies like an arrow, fruit flies like bananas.




More information about the R-help mailing list