[R] Assigning line colors in xyplot

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Thu Aug 30 18:40:38 CEST 2007


On 8/30/07, Christof Bigler <christof.bigler at env.ethz.ch> wrote:
> Hi,
>
> I have a dataframe containing data from individuals 1, ..., 12 (grouping
> variable "g" in the data frame below), which belong either to "A" or "B"
> (grouping variable "f"):
>
> set.seed(1)
>
> tmp <- data.frame(
>
> y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2)),
>       x=1:10,
>
> f=c(rep("A",10),rep("B",10),rep("A",10),rep("B",10),rep("A",10),rep("B",10),rep("A",10),rep("B",10),rep("A",10),rep("B",10),rep("A",10),rep("B",10)),
>
> g=c(rep("3",10),rep("2",10),rep("1",10),rep("4",10),rep("5",10),rep("6",10),rep("8",10),rep("7",10),rep("9",10),rep("11",10),rep("12",10),rep("10",10)))
>
>
> I would like to draw line plots using the function xyplot:
>
> library(lattice)
>
> xyplot(y ~ x | g , groups=g, data=tmp,type="l",
>       par.settings=list(superpose.line=list(col=c("red","blue"))),
>       auto.key=list(space="top",
> text=levels(tmp$f),points=FALSE,lines=TRUE))
>
> As it is, the colors are recycled alternately in the order the
> individuals appear in the plot (1, 10, 11, 12, 2, ..., 9).
>
> How can I assign the red color to all individuals of group "A" and the
> blue color to all individuals of group "B"?

Why not simply use f as the grouping variable rather than g:

xyplot(y ~ x | g , groups=f, data=tmp,type="l",
      par.settings=list(superpose.line=list(col=c("red","blue"))),
      auto.key=list(space="top", points=FALSE,lines=TRUE))

? Or am I missing something?

-Deepayan



More information about the R-help mailing list