[R] lattice: limits in reversed order with relation="same"

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Aug 30 21:42:23 CEST 2010


On Mon, Aug 30, 2010 at 5:51 AM, Thaler,Thorn,LAUSANNE,Applied
Mathematics <Thorn.Thaler at rdls.nestle.com> wrote:
>> Well, it's actually lattice:::extend.limits(range(x)), but
>> extendrange() does basically the same thing, and is available to the
>> user (i.e., exported), albeit from a different package.
>
> Thanks again Deepayan for the insight.
>
> A followup question though-- in another setting I'd like to have relation="free" to allow for different x scales. Thus, I can use the prepanel function:
>
> myprepanel <- function(x,y,...) list(xlim=rev(range(x)))
>
> rep(1:10, 100)
> z <- factor(sample(10, 1000, T))
> y <- rnorm(1000, x, as.numeric(z))
>
> xyplot(y~x|z, scales=list(x="free"), prepanel=myprepanel)
>
> which works as expected. But now I want to reverse the x-axis only for specific panels (z %in% 1:9 say). In the above example 'myprepanel' is called 10 times (well actually it is called nlevels(z) times). Hence, my approach would be to include an 'if' clause in the prepanel function and reverse the xlims only for the appropriate panels.
>
> What I usually do in such cases in panel functions (not beautiful but working though) is something like
>
> if (panel.number() == 1)
>  do something
>
> I'm aware that this is maybe not in the spirit of lattice and that this kind of code maybe breaks as soon as the ordering of panels may change. However, it does the trick for panel functions. It does, however, not work for the prepanel since panel.number() is not defined at this stage (well, actually it returns a 0x0 matrix).
>
> Thus, I've three questions:
>
> 1.) How can I achieve different xlims for different panels in an _automatic_ way? What I've done so far is something like
>
> xyplot(y~x|z, scales=list(x="free"), prepanel=myprepanel, xlim=list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, extendrange(x[z==10])))
>
> Thus, for the first 9 panels the xlims are calculated automatically with myprepanel while for the last one I've to provide the limits explicitly. Is there a more elegant way to do this?
>
> 2.) Within a prepanel function: do I have any possibility to get the information for which panel the prepanel function is called?
>
> 2.) Is the only way to adjust the panel function for different panels using panel.number() or is there another (better) way?
>

That is the recommended way for panels.

The only way really to keep track of prepanel calls is to use an
external counter. E.g.,

prepanel.counter <- 0

myprepanel <- function(x,y,...) {
    prepanel.counter <<- prepanel.counter + 1
    list(xlim=rev(range(x)))
}

-Deepayan



More information about the R-help mailing list