Skip to content

lattice: index plot

4 messages · Peter Ehlers, Thaler, Thorn, LAUSANNE, Applied Mathematics

#
Dear all,

How can I make an index plot with lattice, that is plotting a vector
simply against its particular index in the vector, i.e. something
similar to 

y <- rnorm(10)
plot(y)

I don't want to specify the x's manually, as this could become
cumbersome when having multiple panels.

I tried something like

library(lattice)
mp <- function(x, y, ...) {
  x <- 1:length(y)
  panel.xyplot(x, y, ...)
}

pp <- function(x, y, ...) {
  list(xlim = extendrange(1:length(y)), ylim = extendrange(y))
}

set.seed(123)
y <- rnorm(10)
xyplot(y ~ 1, panel = mp, prepanel = pp, xlab="Index")

but I was wondering whether there is a more straightforward way?

By the way, if I do not specify the ylim in the prepanel function the
plot is clipped, but reading Deepayan's book, p.140 :

"[...], so a user-specified prepanel function is not required to return
all of these components [i.e. xlim, ylim, xat, yat, dx and dy]; any
missing component will be replaced by the corresponding default."

I'd understand that if I do not specify ylim it is calculated
automatically? Not a big thing though, but it seems to me to be
inconsistent.

Any help appreciated. 

KR,

-Thorn
#
Does

  xyplot(y ~ seq_along(y), xlab = "Index")

do what you want?

Peter Ehlers
On 2011-08-02 09:07, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote:
#
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
#
On 2011-08-03 00:24, Thaler,Thorn,LAUSANNE,Applied Mathematics wrote:
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