[R] lattice yscale.components: use multiple convenience functions

David Winsemius dwinsemius at comcast.net
Fri Aug 9 22:01:18 CEST 2013


On Aug 9, 2013, at 12:50 PM, Taylor, Sean D wrote:

> Not sure if this what you were recommending that I do, but based on what I saw in figures 8.4 and 8.5, I was able to combine the yscale.components.log10ticks function definition with that of yscale.components.logpower into my own custom function:

Yep. That what I was recommending. And to answer the question at the end, read ?Startup and add it to your .Rprofile file in your working directory of choice or your Rprofile.site to have it always available. I have code (probably copied from an Rhelp posting that I can no longer attribute properly)  that applies a timestamp to the left side of all of my lattice plots:

lattice.options(default.args = list(page = function(n) {
   panel.text(lab = sprintf("%s", date()), x = 0.01, y = 0.01, adj = 0, srt=90)
}))

-- 
David.

> 
> ###default .log10ticks:
>> yscale.components.log10ticks
> function (lim, logsc = FALSE, at = NULL, ...) 
> {
>    ans <- yscale.components.default(lim = lim, logsc = logsc, 
>        at = at, ...)
>    if (is.null(at)) 
>        return(ans)
>    if (identical(logsc, FALSE)) 
>        return(ans)
>    logbase <- logsc
>    if (identical(logbase, TRUE)) 
>        logbase <- 10
>    if (identical(logbase, "e")) 
>        logbase <- exp(1)
>    tick.at <- logTicks(logbase^lim, loc = 1:9)
>    tick.at.major <- logTicks(logbase^lim, loc = 1)
>    major <- tick.at %in% tick.at.major
>    ans$left$ticks$at <- log(tick.at, logbase)
>    ans$left$ticks$tck <- ifelse(major, 1, 0.5)
>    ans$left$labels$at <- log(tick.at, logbase)
>    ans$left$labels$labels <- as.character(tick.at)
>    ans$left$labels$labels[!major] <- ""
>    ans$left$labels$check.overlap <- FALSE
>    ans
> }
> 
> ####default .logpower:
>> yscale.components.logpower
> function (lim, ...) 
> {
>    ans <- yscale.components.default(lim, ...)
>    ans$left$labels$labels <- parse(text = ans$left$labels$labels)
>    ans
> }
> 
> 
> #####Combine the two functions:
> yscale.components.logpowerTicks<-function (lim, logsc = FALSE, at = NULL, ...) 
> {
>  ans <- yscale.components.default(lim = lim, logsc = logsc, 
>                                   at = at, ...)
>  if (is.null(at)) 
>    return(ans)
>  if (identical(logsc, FALSE)) 
>    return(ans)
>  logbase <- logsc
>  if (identical(logbase, TRUE)) 
>    logbase <- 10
>  if (identical(logbase, "e")) 
>    logbase <- exp(1)
>  tick.at <- logTicks(logbase^lim, loc = 1:9)
>  tick.at.major <- logTicks(logbase^lim, loc = 1)
>  major <- tick.at %in% tick.at.major
>  ans$left$ticks$at <- log(tick.at, logbase)
>  ans$left$ticks$tck <- ifelse(major, 1, 0.5)
>  ans$left$labels$at <- log(tick.at, logbase)
> 
> ####This is the line that I changed
>    ans$left$labels$labels <- parse(text=(paste0(logbase, '^', ans$left$labels$at)))
> ######
> 
>  ans$left$labels$labels[!major] <- ""
>  ans$left$labels$check.overlap <- FALSE
>  ans
> }
> 
> ##It seems that if you define this yourself, you also have to explicitly define this helper function:
> 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]]
> }
> 
> It seems to do what I want:
> xyplot((1:200)/20 ~ (1:200)/20, type = c("p", "g"),
>       scales = list(x = list(log = 10), y = list(log = 10)),
>       yscale.components = yscale.components.logpowerTicks)
> 
> Is there a way to automatically load this function when I start R so it is always available when I want it?
> 
> Thanks,
> Sean
> 
> 
>> -----Original Message-----
>> From: David Winsemius [mailto:dwinsemius at comcast.net]
>> Sent: Tuesday, August 06, 2013 10:54 AM
>> To: Taylor, Sean D
>> Cc: r-help at r-project.org
>> Subject: Re: [R] lattice yscale.components: use multiple convenience
>> functions
>> 
>> 
>> On Aug 6, 2013, at 9:09 AM, Taylor, Sean D wrote:
>> 
>>> Good morning,
>>> 
>>> I really enjoy some of the recent convenience functions in lattice_0.20-15
>> and latticeExtra_0.6-24. I am wondering if there is a way to use multiple
>> functions in the same call? Specifically, I would like to be able to use
>> yscale.components.log10ticks (to get the major tick marks at powers of 10
>> and minor tick marks in between) and also label the major tick marks smartly
>> using superscripts for the power. Something along the lines of this:
>> 
>> See the code supporting figures 8.4 and 8.5 in Sarkar's Lattice book.
>> 
>> --
>> David.
>>> 
>>> ##Pseudocode, does not work
>>> xyplot((1:200)/20 ~ (1:200)/20, type = c("p", "g"),
>>>      scales = list(x = list(log = 2), y = list(log = 10)),
>>>      xscale.components = xscale.components.fractions,
>>>      yscale.components = list(yscale.components.log10ticks,
>>>                               yscale.components.logpower))
>>> 
>>> or this:
>>> ##Does not work
>>> xyplot((1:200)/20 ~ (1:200)/20, type = c("p", "g"),
>>>      scales = list(x = list(log = 2), y = list(log = 10)),
>>>      xscale.components = xscale.components.fractions,
>>>      yscale.components = function(...){
>>>        yscale.components.log10ticks
>>>        yscale.components.logpower}
>>>      )
>>> 
>>> Thanks!
>>> Sean
>>> 
>>> Sean Taylor
>>> Post-doctoral Fellow
>>> Fred Hutchinson Cancer Research Center
>>> 206-667-5544
>>> 
>>> 
>>> 	[[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> 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.
>> 
>> David Winsemius
>> Alameda, CA, USA
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list