I would like to use lattice graphics to plot multiple functions (or groups or subpopulations) on the same plot region, using different line types "lty" or colors "col" to distinguish the functions (or groups). In traditional graphics, this seems straightforward: First plot all the data using 'type="n"', and subsequently execute a series of "points" or "lines" commands, one for each different group or function. What is the elegant way to do this using xyplot? To make this concrete, consider the following toy example: k<- 10 x<- (1:k)/3 yM<-6 + x^2 yF<-12 + x^(1.5) xNA<-x[length(x)] # Insertion of NA row is necessary to prevent a meaningless line # from being drawn from the females to the males across the entire plot. DAT<-data.frame( x=c(x, xNA, x) , y=c(yF, NA, yM) , sex=c( rep(0, k ), 0, rep(1, k)) ) library("lattice") xyplot( y ~ x , data=DAT , type="l" ) This draws both men and women in the same color and line type. Instead, I want to specify different "col" and "lty" values for the two groups. More generally, does a reference exist that explains this kind of thing, with examples? I have not yet found an answer to this question in Paul Murrell's book. Does Deepayan Sarkar's _Lattice_ go into that kind of detail? (I use lattice, not traditional graphics, because the plot will eventually be conditioned on a third categorical variable.) Thanks for any suggestions. Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin@vcu.edu URL: http://www.people.vcu.edu/~jwegelin [[alternative HTML version deleted]]
On Wed, Aug 5, 2009 at 1:30 PM, Jacob Wegelin <jacob.wegelin@gmail.com>wrote:> I would like to use lattice graphics to plot multiple functions (or groups > or subpopulations) on the same plot region, using different line types "lty" > or colors "col" to distinguish the functions (or groups). > > In traditional graphics, this seems straightforward: First plot all the > data using 'type="n"', and subsequently execute a series of "points" or > "lines" commands, one for each different group or function. > > What is the elegant way to do this using xyplot? >> To make this concrete, consider the following toy example: > > k<- 10 > x<- (1:k)/3 > yM<-6 + x^2 > yF<-12 + x^(1.5) > xNA<-x[length(x)] > > # Insertion of NA row is necessary to prevent a meaningless line > # from being drawn from the females to the males across the entire plot. > > DAT<-data.frame( > x=c(x, xNA, x) > , > y=c(yF, NA, yM) > , > sex=c( rep(0, k ), 0, rep(1, k)) > ) > > library("lattice") >Solution (easier than I had imagined): myPanel<-function( x, y ) { panel.xyplot(x, y, type="n") llines( x[DAT$sex==0], y[DAT$sex==0], col="red", lty=1 ) llines( x[DAT$sex==1], y[DAT$sex==1], col="blue", lty=2 ) } xyplot( y ~ x , data=DAT , type="l" , panel=myPanel ) Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin@vcu.edu URL: http://www.people.vcu.edu/~jwegelin [[alternative HTML version deleted]]
On 8/5/09, Jacob Wegelin <jacob.wegelin at gmail.com> wrote:> I would like to use lattice graphics to plot multiple functions (or groups > or subpopulations) on the same plot region, using different line types "lty" > or colors "col" to distinguish the functions (or groups). > > In traditional graphics, this seems straightforward: First plot all the data > using 'type="n"', and subsequently execute a series of "points" or "lines" > commands, one for each different group or function. > > What is the elegant way to do this using xyplot? > > To make this concrete, consider the following toy example: > > k<- 10 > x<- (1:k)/3 > yM<-6 + x^2 > yF<-12 + x^(1.5) > xNA<-x[length(x)] > > # Insertion of NA row is necessary to prevent a meaningless line > # from being drawn from the females to the males across the entire plot. > > DAT<-data.frame( > x=c(x, xNA, x) > , > y=c(yF, NA, yM) > , > sex=c( rep(0, k ), 0, rep(1, k)) > )It's much simpler in lattice, and you don't need to play such tricks. Option 1: xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines = TRUE)) and if you want to control lty etc: xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines = TRUE), par.settings = simpleTheme(lty = c(2, 3))) Option 2 (a bit more work, but less mysterious under the hood): DAT<- data.frame(x = c(x, x), y=c(yF, yM), sex= rep(c("Female", "Male"), each = length(x))) xyplot(y ~ x, data = DAT, groups = sex, type = "l") -Deepayan [...]> This draws both men and women in the same color and line type. Instead, I > want to specify different "col" and "lty" values for the two groups. > > More generally, does a reference exist that explains this kind of thing, > with examples? I have not yet found an answer to this question in Paul > Murrell's book. Does Deepayan Sarkar's _Lattice_ go into that kind of > detail?Yes, but for a shorter introduction, see http://www.bioconductor.org/workshops/2008/PHSIntro/latticeLab.pdf -Deepayan
Hi RUsers I like to keep the plots self contained and avoid changing the current device parameters by using the par.settings. To see what I could achieve by using par settings I tried the following and several variants but could not get black points. xyplot(yM + yF ~ x, panel = panel.superpose, type = c("l", "p"), distribute.type = TRUE, par.settings = list(superpose.line = list(lty = c(1,2), col = c("black","black")), superpose.points = list(pch = c(1,1), col = c("black","black")), plot.symbol = list(pch = c(1,1), col = c("black","black")) ), key = list(text = list(c("male", "female")), lines = Rows(pset$superpose.line, 1:2), pch = 1, type = c("l", "p"))) What am I missing? Does the points reference refer to Grid settings? Regards Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2350 Email Home: mackay at northnet.com.au R version 2.9.1 (2009-06-26) i386-pc-mingw32 attached base packages: [1] datasets utils stats graphics grDevices grid methods base other attached packages: [1] R.oo_1.4.8 R.methodsS3_1.0.3 foreign_0.8-36 chron_2.3-30 MASS_7.2-47 lattice_0.17-25 At 11:48 6/08/2009, Deepayan Sarkar wrote:>On Wed, Aug 5, 2009 at 2:24 PM, Jacob Wegelin<jacob.wegelin at gmail.com> >wrote: > On Wed, 5 Aug 2009, Deepayan Sarkar wrote: > > >> On 8/5/09, Jacob Wegelin <jacob.wegelin at gmail.com> wrote: > >>> I would like to use lattice graphics to plot multiple functions (or > > groups > >>> ? or subpopulations) on the same plot region, using different line types > > "lty" > >>> ? or colors "col" to distinguish the functions (or groups). > >>> > >>> ? In traditional graphics, this seems straightforward: First plot all > the > > data > >>> ? using 'type="n"', and subsequently execute a series of "points" or > > "lines" > >>> ? commands, one for each different group or function. > >>> > >>> ? What is the elegant way to do this using xyplot? > >>> > >>> ? To make this concrete, consider the following toy example: > >>> > >>> ? k<- 10 > >>> ? x<- (1:k)/3 > >>> ? yM<-6 + x^2 > >>> ? yF<-12 + x^(1.5) > >>> ? xNA<-x[length(x)] > >>> > >>> ? # Insertion of NA row is necessary to prevent a meaningless line > >>> ? # from being drawn from the females to the males across the entire > plot. > >>> > >>> ? DAT<-data.frame( > >>> ? x=c(x, xNA, x) >>> ? , >>> ? y=c(yF, NA, yM) >>> ? , >>> ? sex=c( > rep(0, k ), 0, ? rep(1, k)) >>> ? ) >> >> It's much simpler in lattice, > and you don't need to play such tricks. > Option 1: >> >> xyplot(yM + yF > ~ x, type = "l", auto.key = list(points = FALSE, lines = > TRUE)) >> >> > and if you want to control lty etc: >> >> xyplot(yM + yF ~ x, type = "l", > auto.key = list(points = FALSE, lines = > TRUE), >> ? ? ? par.settings > = simpleTheme(lty = c(2, 3))) >> >> >> Option 2 (a bit more work, but > less mysterious under the hood): >> >> DAT<- >> ? ? data.frame(x = c(x, > x), y=c(yF, yM), >> ? ? ? ? ? ? ? sex= rep(c("Female", "Male"), > each = length(x))) >> >> xyplot(y ~ x, data = DAT, groups = sex, type = > "l") > > Dear Bill and Deepayan, > > Thanks. This is helpful. Where can > one find a thorough documentation of all > these features like > par.settings, simpleTheme, the options for where to > place the legend or > "key", auto.key, the different locations besides "top" > where one can > place the "auto.key", etc.? ? I don't think this is all clearly > laid > out in the R help files or latticeLab.pdf. (Almost) everything is > mentioned in the help pages (?Lattice is a good place to start). Of > course finding the thing you are looking for is another matter. The book > does try to present things more systematically. > But using your hints I > found that the following worked: > > xyplot( > y ~ x > , groups= ~ sex > > , type="l" > , auto.key = list(columns=2, points = FALSE, lines = TRUE) > > , par.settings = simpleTheme(lty = c(1, 2), col="black") > , data=DAT > > ) > > Now, how would I use lattice tools to plot males with a line and > females > with points--and still get an informative autokey? > >xyplot(yM + yF ~ x, > type = c("l", "p"), > distribute.type = TRUE, > par.settings = simpleTheme(lty = c(1, 2), col="black"), > auto.key = list(points = FALSE, lines = TRUE, type = c("l", "p"))) > >...but this is pretty much impossible to figure out for a beginner. >On the other hand, reading the documentation carefully should lead you to >the following, which is almost there: > >pset <- simpleTheme(lty = c(1, 2), col="black") >xyplot(yM + yF ~ x, > panel = panel.superpose, > type = c("l", "p"), > distribute.type = TRUE, > par.settings = pset, > key = list(text = list(c("male", "female")), > lines = Rows(pset$superpose.line, 1:2), > pch = 1, > type = c("l", "p"))) > >-Deepayan >______________________________________________ >R-help at 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.
Hi Jake the pset is the same as that of Deepayan Sarkar's. I must not have copied all the code as I thought and did not see that there was no pset Basically I am trying to keep everything contained within the xyplot and avoid changing the device settings. I frequently use par.settings = list( ... and put any amendments that I need from trellis.par.get() there- very easy to get an idea of what is where by names(trellis.par.get()) and then calling the particular name that you want to see what to change Regards Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2350 Email Home: mackay@northnet.com.au At 04:31 9/08/2009, you wrote:>I tried pasting your code into R and got an error on pset: > > > xyplot(yM + yF ~ x, >+ panel = panel.superpose, >+ type = c("l", "p"), >+ distribute.type = TRUE, >+ par.settings = list(superpose.line = list(lty = c(1,2), >+ col = c("black","black")), >+ superpose.points = list(pch = c(1,1), col = >c("black","black")), >+ plot.symbol = list(pch = c(1,1), col = >c("black","black")) >+ ), >+ key = list(text = list(c("male", "female")), >+ lines = Rows(pset$superpose.line, 1:2), >+ pch = 1, >+ type = c("l", "p"))) >Error in Rows(pset$superpose.line, 1:2) : object "pset" not found > >Jake Wegelin > > > >On Fri, Aug 7, 2009 at 6:53 PM, Duncan Mackay ><<mailto:mackay@northnet.com.au>mackay@northnet.com.au> wrote: >Hi RUsers > >I like to keep the plots self contained and avoid changing the current >device parameters by using the par.settings. >To see what I could achieve by using par settings I tried the following >and several variants but could not get black points. > >xyplot(yM + yF ~ x, > panel = panel.superpose, > type = c("l", "p"), > distribute.type = TRUE, > par.settings = list(superpose.line = list(lty = c(1,2), > col = c("black","black")), > superpose.points = list(pch = c(1,1), col = > c("black","black")), > plot.symbol = list(pch = c(1,1), col = > c("black","black")) > ), > key = list(text = list(c("male", "female")), > lines = Rows(pset$superpose.line, 1:2), > pch = 1, > type = c("l", "p"))) > >What am I missing? Does the points reference refer to Grid settings? > >Regards > >Duncan Mackay >Department of Agronomy and Soil Science >University of New England >Armidale NSW 2350 >Email Home: <mailto:mackay@northnet.com.au>mackay@northnet.com.au > >R version 2.9.1 (2009-06-26) >i386-pc-mingw32 > >attached base packages: >[1] datasets utils stats graphics grDevices grid methods base >other attached packages: >[1] R.oo_1.4.8 R.methodsS3_1.0.3 >foreign_0.8-36 chron_2.3-30 MASS_7.2-47 lattice_0.17-25 > > > >At 11:48 6/08/2009, Deepayan Sarkar wrote: >On Wed, Aug 5, 2009 at 2:24 PM, Jacob >Wegelin<<mailto:jacob.wegelin@gmail.com>jacob.wegelin@gmail.com> wrote: > >On Wed, 5 Aug 2009, Deepayan Sarkar wrote: > > >> On 8/5/09, Jacob Wegelin > <<mailto:jacob.wegelin@gmail.com>jacob.wegelin@gmail.com> wrote: > >>> I would like to use lattice graphics to plot multiple functions (or > > groups > >>> Â or subpopulations) on the same plot region, using different line types > > "lty" > >>> Â or colors "col" to distinguish the functions (or groups). > >>> > >>> Â In traditional graphics, this seems straightforward: First plot all the > > data > >>> Â using 'type="n"', and subsequently execute a series of "points" or > > "lines" > >>> Â commands, one for each different group or function. > >>> > >>> Â What is the elegant way to do this using xyplot? > >>> > >>> Â To make this concrete, consider the following toy example: > >>> > >>> Â k<- 10 > >>> Â x<- (1:k)/3 > >>> Â yM<-6 + x^2 > >>> Â yF<-12 + x^(1.5) > >>> Â xNA<-x[length(x)] > >>> > >>> Â # Insertion of NA row is necessary to prevent a meaningless line > >>> Â # from being drawn from the females to the males across the entire > plot. > >>> > >>> Â DAT<-data.frame( > >>> Â x=c(x, xNA, x) >>> Â , >>> Â y=c(yF, NA, yM) >>> Â , >>> Â sex=c( > rep(0, k ), 0, Â rep(1, k)) >>> Â ) >> >> It's much simpler in lattice, > and you don't need to play such tricks. > Option 1: >> >> xyplot(yM + yF > ~ x, type = "l", auto.key = list(points = FALSE, lines = > TRUE)) >> >> > and if you want to control lty etc: >> >> xyplot(yM + yF ~ x, type = "l", > auto.key = list(points = FALSE, lines = > TRUE), >> Â Â Â par.settings > = simpleTheme(lty = c(2, 3))) >> >> >> Option 2 (a bit more work, but > less mysterious under the hood): >> >> DAT<- >> Â Â data.frame(x = c(x, > x), y=c(yF, yM), >> Â Â Â Â Â Â Â sex= rep(c("Female", "Male"), > each = length(x))) >> >> xyplot(y ~ x, data = DAT, groups = sex, type = > "l") > > Dear Bill and Deepayan, > > Thanks. This is helpful. Where can > one find a thorough documentation of all > these features like > par.settings, simpleTheme, the options for where to > place the legend or > "key", auto.key, the different locations besides "top" > where one can > place the "auto.key", etc.? Â I don't think this is all clearly > laid > out in the R help files or latticeLab.pdf. (Almost) everything is > mentioned in the help pages (?Lattice is a good place to start). Of > course finding the thing you are looking for is another matter. The book > does try to present things more systematically. > But using your hints I > found that the following worked: > > xyplot( > y ~ x > , groups= ~ sex > > , type="l" > , auto.key = list(columns=2, points = FALSE, lines = TRUE) > > , par.settings = simpleTheme(lty = c(1, 2), col="black") > , data=DAT > > ) > > Now, how would I use lattice tools to plot males with a line and > females > with points--and still get an informative autokey? > >xyplot(yM + yF ~ x, > type = c("l", "p"), > distribute.type = TRUE, > par.settings = simpleTheme(lty = c(1, 2), col="black"), > auto.key = list(points = FALSE, lines = TRUE, type = c("l", "p"))) > >...but this is pretty much impossible to figure out for a beginner. >On the other hand, reading the documentation carefully should lead you to >the following, which is almost there: > >pset <- simpleTheme(lty = c(1, 2), col="black") >xyplot(yM + yF ~ x, > panel = panel.superpose, > type = c("l", "p"), > distribute.type = TRUE, > par.settings = pset, > key = list(text = list(c("male", "female")), > lines = Rows(pset$superpose.line, 1:2), > pch = 1, > type = c("l", "p"))) > >-Deepayan >______________________________________________ ><mailto:R-help@r-project.org>R-help@r-project.org mailing list ><https://stat.ethz.ch/mailman/listinfo/r-help>https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide ><http://www.R-project.org/posting-guide.html>http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > >______________________________________________ ><mailto:R-help@r-project.org>R-help@r-project.org mailing list ><https://stat.ethz.ch/mailman/listinfo/r-help>https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide ><http://www.R-project.org/posting-guide.html>http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Possibly Parallel Threads
- xyplot: several plots in one creates y-scale problem
- lattice 3x3 plot: force common y-limits accross rows and align x-axes
- xyplot: Plotting two variables, one as points - the other as line. Can that be done without explicitly using panel functions
- User defined panel functions in lattice
- Polygon Graph in lattice/ lattice extra