On Wed, Aug 5, 2009 at 2:24 PM, Jacob Wegelin<jacob.wege...@gmail.com> wrote: > On Wed, 5 Aug 2009, Deepayan Sarkar wrote: > >> On 8/5/09, Jacob Wegelin <jacob.wege...@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@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.