Thaler, Thorn, LAUSANNE, Applied Mathematics
2011-Aug-02 16:07 UTC
[R] lattice: index plot
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:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.