[R] lattice: index plot

Thaler, Thorn, LAUSANNE, Applied Mathematics Thorn.Thaler at rdls.nestle.com
Wed Aug 3 09:24:13 CEST 2011


> Does
> 
>   xyplot(y ~ seq_along(y), xlab = "Index")
> 
> do what you want?


Not exactly, because it does not work once multipanel conditioning comes
into play:

xyplot(y~seq_along(y)|factor(rep(1:2, each=5)), xlab = "Index")

The points in the right panel are plotted from 6:10 while the points in
the left panel are plotted from 1:5. Of course I could do something like


xyplot(y~rep(1:5, 2) |factor(rep(1:2, each=5)), xlab = "Index")

in this toy example, but as pointed out this becomes very cumbersome if
the grouping variable does not follow a pattern.

BTW: my toy example did not work with multipanel conditioning either,
but one can work around that too using the subscripts argument in the
panel function (I skipped that exercise for the sake of brevity, but I
must admit that it obscured somehow my real intention, sorry for that).

However, the more I think of it the more I believe that I have to
provide the x's explicitly nevertheless and my solution would be:

set.seed(123)
y <- rnorm(20)
grp <- index <- sample(3, 20, TRUE)
index[unlist(lapply(levels(as.factor(grp)), function(n)
which(as.factor(grp)==n)))] <- unlist(tapply(grp, grp, seq_along)) 
xyplot(y ~ index | factor(grp), xlab = "Index")

This should work, but it seems to be a rather elaborate solution,
especially since an index plot is nothing too fancy.

So maybe I'm not seeing the wood for trees, but does anybody know an
easier way?

Thanks.

KR,

-Thorn



More information about the R-help mailing list