[R] coloring individual points in lattice xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 5 22:37:46 CEST 2006


On 7/5/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> On 7/5/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > On 7/5/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> > > On 7/4/06, Gabor Grothendieck <ggrothendieck at 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



More information about the R-help mailing list