[R] define colors for groups in lattice xyplot

Dieter Menne dieter.menne at menne-biomed.de
Fri Sep 3 10:47:29 CEST 2010


Geert, 

thanks for providing a nice example. When use see "groups" in xyplot, you
should switch to the documentation (and use) panel.superpose. Which has a
somewhat different philosophy (your looks more like ggplot2 would do it).

The docs of panel.superpose say (tersely...)

col: graphical parameters, replicated to be as long as the number of groups.
These are eventually passed down to panel.groups, but as scalars rather than
vectors. When panel.groups is called for the i-th level of groups, the
corresponding element of each graphical parameter is passed to it. 

dataset <- data.frame ( time = rep(1:5,times=9),
         genotype = factor(rep(rep (c("A","B","C"),each=5),times=3
)),
         location= factor(rep (paste("LOC",1:3),each=15)),
            color = rep (rep (c("red","green","blue"),each=5),times=3
),
            result = rnorm (45))
                                           
library(lattice)

# color in the data frame is not needed, just defined it for the group
mycol = c("red","green","blue")

# The complex way with an explicit panel.superpose
xyplot( result ~ time | location, data=dataset,groups=genotype,
                fill.color = dataset$color,
                type="b",               
                panel = function(x, y,...) {
                        panel.superpose(x, y,...,pch=19, col = mycol)
     })

# The easy way out
xyplot( result ~ time | location, data=dataset,groups=genotype,
                fill.color = dataset$color,
                col=mycol, type="b",pch=19 )


Dieter
-- 
View this message in context: http://r.789695.n4.nabble.com/define-colors-for-groups-in-lattice-xyplot-tp2525197p2525321.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list