Re: [R] coloring individual points in lattice xyplot

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Thu 06 Jul 2006 - 06:37:46 EST

On 7/5/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> On 7/5/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > On 7/5/06, Deepayan Sarkar <deepayan.sarkar@gmail.com> wrote:
> > > On 7/4/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > > > OK. It looks like I need to go to the lower level llines and lpoints to
> > > > do this. I wrote a panel routine, mypanel, and it seems to work (see
> > > > below); however, currently it assumes types and cols are in the global
> > > > environment or at least somewhere where they will be found.
> > > >
> > > > 1. Is it somehow possible to stick these into some structures set up by
> > > > lattice already and then retrieve them from lattice from within mypanel?
> > >
> > > Not sure what you mean by that, but I would do something like the
> > > following (this allows 'col' to be a list as in your original post).
> > > Note that arguments to the panel function can be supplied directly to
> > > the high level function (that's why graphical parameters can be
> > > supplied to xyplot in the first place).
> > >
> > > mypanel <-
> > > function(x, y, subscripts, groups,
> > > col = 1,
> > > type = "p",
> > > ...)
> > > {
> > > col <- rep(as.list(col), length = nlevels(groups))
> > > type <- rep(as.list(type), length = nlevels(groups))
> > > for(g in 1:nlevels(groups)) {
> > > idx <- g == groups[subscripts]
> > > xx <- x[idx]; yy <- y[idx];
> > > panel.xyplot(x[idx], y[idx],
> > > col = col[[g]],
> > > type = type[[g]],
> > > ...)
> > > }
> > > }
> > >
> > > xyplot(y ~ c(x,x), groups = factor(col(y)),
> > > panel = mypanel,
> > > type = c("o", "p"),
> > > col = list("black", 1:10))
> >
> >
> > Thanks!!! This is quite an improvement.
> >
> > >
> > >
> > > > 2. Any other improvements to the example below?
> > > >
> > > > mypanel <- function(x, y, subscripts, groups, ...) {
> > > > for(g in 1:nlevels(groups)) {
> > > > idx <- g == groups
> > >
> > > This won't work for more than one panel.

> >
> > Could you explain this? Under what situation does it not work?
> > The following, for example, appears to work:
> >
> > xyplot(y ~ c(x,x) | col(y), groups = factor(col(y)),
> > panel = mypanel,
> > type = c("o", "p"),
> > col = list("black", 1:10),
> > layout = 1:2)

>

> Just to clarify mypanel refers to your version of mypanel.
> Were you saying that your version still does not work in certain
> situations with multiple panels or only mine?

I meant your version. You need the [subscripts] part, which your version didn't have (mine did). This is because unlike the 'x' and 'y' variables, 'groups' doesn't get subsetted before being passed on to the panel function. In fact, apart from the fact that it is evaluated in 'data', 'groups' is in principle just like any other `non-standard' argument, in that it gets passed on to the panel function unchanged.

-Deepayan



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 06 06:42:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 06 Jul 2006 - 08:18:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.