Hi 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) # The black, red, green behaviour you were seeing is because # dataset$color is a factor dataset$color # which is coded 1, 2, 3 levels = ("red", "green", "blue"). # This is being interpreted as the first three colours of the palette pallete() # coerce the vector to a character vector as.character(dataset$color) # gives you xyplot( result ~ time | location, data=dataset,groups=genotype, fill.color = as.character(dataset$color), panel = function(x, y,fill.color,...,subscripts) { fill = fill.color [subscripts] panel.xyplot(x, y,pch=19, col=fill, type ="b")} ) # Lines only take a single value so only the first value of the vector, # "red", is passed to line colour. # these are simple alternatives xyplot( result ~ time | location, data=dataset, groups=genotype, pch=c(1,2,3), type="b", col=c("red","blue","green")) xyplot( result ~ time | location, data=dataset, groups=genotype, pch=c(1,2,3), type="b", col=dataset$color) # or use par.settings <- list(superpose.symbol = list(col = c("red", "green", "blue"), fill = c("red", "green", "blue")), superpose.line = list(col = c("red", "green", "blue")) ) xyplot( result ~ time | location, data=dataset,groups=genotype,pch=19, type = "b", par.settings = par.settings) ### Hope this helps Chris ### Hadley Wickham, Creator of ggplot2 - teaching in the UK. 1st - 2nd November 2010. To book your seat please go to http://mango-solutions.com/news.html Chris Campbell MANGOSOLUTIONS R consulting and training T: +44 (0)1249 767700 Ext: 233 F: +44 (0)1249 767707 M: +44 (0)7967 028876 www.mango-solutions.com Unit 2 Greenways Business Park Bellinger Close Chippenham Wilts SN15 1BN UK -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of geert.deme...@bayercropscience.com Sent: 03 September 2010 07:44 To: r-help@r-project.org Subject: [R] define colors for groups in lattice xyplot 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\ exc...{{dropped:19}} ______________________________________________ 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.