[R] lattice: index plot

Thaler, Thorn, LAUSANNE, Applied Mathematics Thorn.Thaler at rdls.nestle.com
Tue Aug 2 18:07:35 CEST 2011


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



More information about the R-help mailing list