[R] coloring individual points in lattice xyplot
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jul 5 21:56:52 CEST 2006
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)
>
> > xx <- x[idx]; yy <- y[idx]; ccols <- cols[subscripts][idx]
> > if (any(idx)) {
> > switch(types[g],
> > p = lpoints(xx, yy, col = ccols),
> > l = llines(xx, yy, col = ccols),
> > o = { lpoints(xx, yy, col = ccols)
> > llines(xx, yy, col = ccols) })
> > }
> > }
> > }
> >
> > x <- 1:10
> > y <- cbind(y1 = x, y2 = x+1)
> > cols <- c(rep(1,10), 1:10)
> > types <- c("o", "p")
> > xyplot(y ~ c(x,x), groups = factor(col(y)), type = types, panel = mypanel)
> >
> >
> >
> > On 7/4/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> > > On 7/4/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > I can get the types to work or the colors but not both:
> > >
> > > Sorry if I wasn't clear, but I didn't mean that you could use
> > > panel.superpose[.2] to do what you wanted. I only meant that you could
> > > use it as a template that may help you to write your own panel
> > > function. What you want is not possible with tools available in
> > > lattice.
> > >
> > > Deepayan
> > >
> > > > # this gets the types right but not the colors
> > > > library(lattice)
> > > > x <- 1:10
> > > > y <- cbind(y1 = x, y2 = x+1)
> > > > cols <- c(rep(1,10), 1:10)
> > > > xyplot(y ~ c(x,x), groups = col(y), type = c("o", "p"),
> > > > panel = function(x, y, subscripts, groups, ...)
> > > > panel.superpose.2(x, y, subscripts, groups, col = cols[subscripts], ...)
> > > > )
> > > >
> > > >
> > > > # this gets the colors right but not the types
> > > > library(lattice)
> > > > x <- 1:10
> > > > y <- cbind(y1 = x, y2 = x+1)
> > > > cols <- c(rep(1,10), 1:10)
> > > > xyplot(y ~ c(x,x), groups = col(y), type = c("o", "p"),
> > > > panel = function(x, y, subscripts, groups, ...)
> > > > panel.xyplot(x, y, col = cols[subscripts], ...)
> > > > )
> > > >
> > > >
> > > > On 7/4/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > > On 7/4/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> > > > > > On 7/4/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > > > > If I wish to color groups in xyplot I can do this:
> > > > > > >
> > > > > > > library(lattice)
> > > > > > > x <- 1:10
> > > > > > > y <- cbind(x, x+1)
> > > > > > > xyplot(y ~ rep(x,2), group = col(y), col = 1:2)
> > > > > > >
> > > > > > > How do I color different points differently within a group.
> > > > > > >
> > > > > > > For example, I want to produce this plot (except that I only
> > > > > > > want to have two groups, not 11):
> > > > > > >
> > > > > > > xyplot(y ~ rep(x,2), group = c(rep(1, 10), 2:11), col = 1:11)
> > > > > > >
> > > > > > > I am thinking of something like this (although
> > > > > > > this does not work, its just to get the idea across):
> > > > > > >
> > > > > > > xyplot(y ~ rep(x,2), group = col(y), col = list(1, 2:11))
> > > > > > >
> > > > > > > where, in general, I have a list with one component per group
> > > > > > > whose elements are scalars to color the whole group or
> > > > > > > vectors one color per point in the group. I don't know
> > > > > > > ahead of time what the list is.
> > > > > > >
> > > > > > > I am looking for a general approach to this within the lattice
> > > > > > > xyplot plot framework; the above is just an example.
> > > > > >
> > > > > > The general approach is to write your own panel function. For a
> > > > > > possible template, look at the functions panel.superpose and
> > > > > > panel.superpose.2 and how they handle the 'type' argument.
> > > > > >
> > > > > > Deepayan
> > > > > >
> > > > >
> > > > > There is no example in ?panel.superpose. Do you think you
> > > > > could provide an example for the situation in my post?
> > > > >
> > > > > I have done quite a bit of RSiteSearch'ing and googling prior to
> > > > > posting and all the examples I found had colors that depended
> > > > > on the group, none addressed the situation in my post -- i.e.
> > > > > coloring individual points within groups.
> > > > >
> > > >
> > >
> > >
> > > --
> > > http://www.stat.wisc.edu/~deepayan/
> > >
> >
>
>
> --
> http://www.stat.wisc.edu/~deepayan/
>
More information about the R-help
mailing list