[R] lattice: index plot
Peter Ehlers
ehlers at ucalgary.ca
Wed Aug 3 16:16:52 CEST 2011
On 2011-08-03 00:24, Thaler,Thorn,LAUSANNE,Applied Mathematics wrote:
>> 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?
Here's a way to use 'subscripts' in the xyplot.
The main problem is to determine the xlims to use.
dat <- data.frame(y, grp)
## xlims
xL <- function(groups){
tbl <- table(groups)
xlim <- c(0, max(tbl) + 1)
xlim
}
xyplot(y ~ seq_along(y) | factor(grp), data = dat,
xlim = xL(dat$grp),
panel = function(y, subscripts){
x <- seq_along(subscripts)
panel.xyplot(x, y)
}
)
Peter Ehlers
>
> Thanks.
>
> KR,
>
> -Thorn
>
>
>
More information about the R-help
mailing list