[R] using perm.cond & index.cond in lattice package

Deepayan Sarkar deepayan at stat.wisc.edu
Mon Jul 5 20:02:57 CEST 2004


On Monday 05 July 2004 09:57, 8rino-Luca Pantani wrote:
> Dear R users,
> this is my first question to the list.
> I hope it will be not a trivial one.
>
> My problem is to change the order in which the panel are plotted in a
> lattice/Trellis plot.
> I've read the S-plus Trellis Graphics user manual, in which there is
> a function called "reorder.factor",

Hmm, I didn't know about this function, but it seems easy enough to 
define. In fact the groupedData() constructor in nlme does this, and 
that can be adapted to have

reorder.factor <- function(Factor, X, Function = mean, ...)
    ordered(Factor,
            levels = names(sort(tapply(X, Factor, Function, ...))))

which you may then use. 

> that, as far as I can catch from ?xyplot, in the lattice package is
> substituted by two others:
> "index.cond" & "perm.cond"

[...]

> bwplot(Y~TREAT|LOCATION, data=mydframe)
> #The panels are plotted in alphabetical order, but I would rather
> have something like
> bwplot(Y~TREAT|OM, data=mydframe)
> # i.e. the panels ordered by OM (Organic Matter) content
> #but with LOCATION written in the strip.
> #From
> #str(bwplot(Y~TREAT|OM, data=mydframe))
> #I can see that
> # $ index.cond       :List of 1
> # ..$ : int [1:4] 1 2 3 4
> # $ perm.cond        : num 1
> #so I tried
> bwplot(Y~TREAT|LOCATION,
>        data=mydframe,
>        index.cond=
> list((1:4)[order(unique(mydframe$OM[order(mydframe$LOCATION)]))])
>        )

You might as well just have

list(order(unique(mydframe$OM[order(mydframe$LOCATION)])))

> #and it seems to work, but without the darker tag for OM in the
> strips.

The darker portion has to do with the default 'style' in the strip 
function. It's always there for numeric (shingle) variables. For 
factors, S-PLUS defaults to style=3, which shows the darker parts, 
while lattice default to style=1, which doesn't.

> Now I have one perplexity and one question:
>
> The last "list((1:4)[order(...." works fine, but it seems to me
> inelegant, too complicate and not practical for plots with more
> factors. Any suggestion to simplify/improve the matter?

As a matter of fact, I'm working on something like this for a future 
version of lattice. But in your example, where you know beforehand how 
your factor levels should be ordered, I think you should just define 
your factor properly. e.g.,

LOCATION = factor(rep(rep(c(5, 1, 3, 7), each=3), 2),
                  levels = c(1, 3, 5, 7), 
                  labels = c("there1%OM", "elsewhere3%OM", 
                             "here5%OM", "overthere7%OM"))

What's happening now is that LOCATION is defined as a character vector, 
which is eventually coerced to a factor with levels in the default (in 
this case undesirable) order. 

> How can I keep the tags in the strips, still ordering the panels by
> OM?

You need to add something like

  strip = function(..., style) strip.default(..., style = 3)

to your bwplot call.


> Thanks in advance.
>
> PS
> The following lines are copied from ?xyplot
> (I suspect that "order.cond" stands for "index.cond",
> since I cannot find it in any other page of the lattice help, nor in
> the general help)

You are right. I'll fix that.

Deepayan




More information about the R-help mailing list