geert.demeyer at bayercropscience.com
2010-Sep-03 06:43 UTC
[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 excl...{{dropped:14}}
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}}
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.demeyer@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}}
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.