Re: [R] Assigning line colors in xyplot
Thanks to Deepayan and Hadley for their help, both solutions work, not only for the example below, but also for my real data set that I'm about to analyze. Christof On 01.09.2007, at 00:32, Deepayan Sarkar wrote: On 8/31/07, Christof Bigler [EMAIL PROTECTED] wrote: The suggestions by Deepayan Sarkar and Hadley Wickham work for that case, but I get into troubles when I try to draw e.g. a panel for A and B: xyplot(y ~ x | f , 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)) Similarly for xyplot(y ~ x | f , groups=f, ...) Let's assume that we use a slightly more complex dataframe, where we have different individuals a, ..., f (grouping variable h) with 1 or 2 series of measurements per individual (grouping variable g). The individuals are grouped as A or B (grouping variable f) according to the distribution of the values in the variable y. set.seed(1) tmp2 - data.frame( y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,4,2),rnorm (10,0,1),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1)), x=1:10, f=c(rep(A,10),rep(B,10),rep(B,10),rep(B,10),rep(A,10),rep (A,10),rep(A,10),rep(B,10),rep(A,10)), g=c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep (6,10),rep(7,10),rep(8,10),rep(9,10)), h=c(rep(a,10),rep(b,10),rep(b,10),rep(c,10),rep(d,10),rep (d,10),rep(e,10),rep(f,10),rep(e,10))) Again, the line colors in the following plot are correct: xyplot(y ~ x | g , groups=f, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, points=FALSE,lines=TRUE)) But, if I want to show e.g. one panel per individual, the assignment of the colors is not correct: xyplot(y ~ x | h , groups=g, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Any suggestions? This works (only the 'groups' argument is different from your call): xyplot(y ~ x | h , groups=interaction(f, g), data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Note that unlike ggplot, you cannot (easily) use a different variable for grouping and coloring. Fortunately, the levels of interaction(f, g) are such that your colors (red, blue) are repeated in the right order, so you don't need to do much extra work. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
The suggestions by Deepayan Sarkar and Hadley Wickham work for that case, but I get into troubles when I try to draw e.g. a panel for A and B: xyplot(y ~ x | f , 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)) Similarly for xyplot(y ~ x | f , groups=f, ...) Let's assume that we use a slightly more complex dataframe, where we have different individuals a, ..., f (grouping variable h) with 1 or 2 series of measurements per individual (grouping variable g). The individuals are grouped as A or B (grouping variable f) according to the distribution of the values in the variable y. set.seed(1) tmp2 - data.frame( y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1)), x=1:10, f=c(rep(A,10),rep(B,10),rep(B,10),rep(B,10),rep(A,10),rep(A,10),rep(A,10),rep(B,10),rep(A,10)), g=c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep(6,10),rep(7,10),rep(8,10),rep(9,10)), h=c(rep(a,10),rep(b,10),rep(b,10),rep(c,10),rep(d,10),rep(d,10),rep(e,10),rep(f,10),rep(e,10))) Again, the line colors in the following plot are correct: xyplot(y ~ x | g , groups=f, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, points=FALSE,lines=TRUE)) But, if I want to show e.g. one panel per individual, the assignment of the colors is not correct: xyplot(y ~ x | h , groups=g, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Any suggestions? Christof [EMAIL PROTECTED] wrote: On 8/30/07, Christof Bigler [EMAIL PROTECTED] 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 __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
On 8/31/07, Christof Bigler [EMAIL PROTECTED] wrote: The suggestions by Deepayan Sarkar and Hadley Wickham work for that case, but I get into troubles when I try to draw e.g. a panel for A and B: xyplot(y ~ x | f , 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)) In ggplot, you would need to specify the grouping variable as well as the colour variable: qplot(x, y, data=tmp, geom=line, colour=f, group=interaction(f,g)) which should work for any facetting. Hadley __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
On 8/31/07, Christof Bigler [EMAIL PROTECTED] wrote: The suggestions by Deepayan Sarkar and Hadley Wickham work for that case, but I get into troubles when I try to draw e.g. a panel for A and B: xyplot(y ~ x | f , 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)) Similarly for xyplot(y ~ x | f , groups=f, ...) Let's assume that we use a slightly more complex dataframe, where we have different individuals a, ..., f (grouping variable h) with 1 or 2 series of measurements per individual (grouping variable g). The individuals are grouped as A or B (grouping variable f) according to the distribution of the values in the variable y. set.seed(1) tmp2 - data.frame( y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1)), x=1:10, f=c(rep(A,10),rep(B,10),rep(B,10),rep(B,10),rep(A,10),rep(A,10),rep(A,10),rep(B,10),rep(A,10)), g=c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep(6,10),rep(7,10),rep(8,10),rep(9,10)), h=c(rep(a,10),rep(b,10),rep(b,10),rep(c,10),rep(d,10),rep(d,10),rep(e,10),rep(f,10),rep(e,10))) Again, the line colors in the following plot are correct: xyplot(y ~ x | g , groups=f, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, points=FALSE,lines=TRUE)) But, if I want to show e.g. one panel per individual, the assignment of the colors is not correct: xyplot(y ~ x | h , groups=g, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Any suggestions? This works (only the 'groups' argument is different from your call): xyplot(y ~ x | h , groups=interaction(f, g), data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Note that unlike ggplot, you cannot (easily) use a different variable for grouping and coloring. Fortunately, the levels of interaction(f, g) are such that your colors (red, blue) are repeated in the right order, so you don't need to do much extra work. -Deepayan __ R-help@stat.math.ethz.ch 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.
[R] Assigning line colors in xyplot
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? Thanks in advance! Christof __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
Hi Christof, You can do this in ggplot, with one exception: install.packages(ggplot2) library(ggplot2) qplot(x, y, data=tmp, facets = . ~ g, geom=line, colour=f) Unfortunately I don't yet have an implementation of facetting that works like lattice, wrapping the line of plots in to 2d dimensions. You can find out more about ggplot2 at http://had.co.nz/ggplot2 Hadley On 8/30/07, Christof Bigler [EMAIL PROTECTED] 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? Thanks in advance! Christof __ R-help@stat.math.ethz.ch 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. -- http://had.co.nz/ __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
On 8/30/07, Christof Bigler [EMAIL PROTECTED] 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 __ R-help@stat.math.ethz.ch 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.