Hi:

The 'easy' solution is to define the colors corresponding to the genotypes
directly in lattice:

xyplot( result ~ time | location, data=dataset, groups=genotype, pch=19,
type="b", col = c('red', 'green', 'blue'))

The problem with trying to use color as a variable to 'match' to genotype is
that both genotype and color are factors, but the default ordering of factor
levels is alphabetic. This is no problem for genotype, but it is for color,
so the matching of genotype-color pairs in dataset doesn't actually occur;
instead, R will render colors blue, green and red, respectively, to match to
genotypes A-C. To fix that problem, make color an ordered factor:

dataset$color <- ordered(dataset$color, levels = c('red', 'green', 'blue'))
xyplot( result ~ time | location, data=dataset, groups=genotype, pch=19,
   type="b", col = levels(dataset$color))

HTH,
Dennis



On Thu, Sep 2, 2010 at 11:43 PM, <geert.deme...@bayercropscience.com> wrote:

> Dear all,
>
> Lattice provides automatic coloring for subgroups on each panel by the
> simple use of a groups statement. For an application I want to change
> these colors to a predifined set. This works well using a panel function
> in stead of the default as long as there are only points in the graphs.
> When I set type="b" things get messed up. Any idea why? I include sample
> code for illustration below.
>
> Thanks for your ideas.
>
> Geert
>
> 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)
>
> xyplot( result ~ time | location, data=dataset,groups=genotype,pch=19,
> type="b")
>
> xyplot( result ~ time | location, data=dataset,groups=genotype,
>                fill.color = dataset$color,
>                panel = function(x, y,fill.color,...,subscripts) {
>                        fill = fill.color [subscripts]
>                        panel.xyplot(x, y,pch=19, col=fill)}
> )
>
> xyplot( result ~ time | location, data=dataset,groups=genotype,
>                fill.color = dataset$color,
>                panel = function(x, y,fill.color,...,subscripts) {
>                        fill = fill.color [subscripts]
>                        panel.xyplot(x, y,pch=19, col=fill, type ="b")}
> )
> ________________________________________________________________________
> The information contained in this e-mail is for the ex...{{dropped:13}}

______________________________________________
R-help@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.

Reply via email to