[R] define colors for groups in lattice xyplot
Chris Campbell
ccampbell at mango-solutions.com
Fri Sep 3 10:43:04 CEST 2010
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-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of geert.demeyer at bayercropscience.com
Sent: 03 September 2010 07:44
To: r-help at 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}}
More information about the R-help
mailing list