Ok...I realized that if I don't wish to compare the groupings directly i can plot the groups as separate panels, which is a much more simple solution, but doesn't necessarily give the same effect.
xyplot(y+y2 ~ x | grp, dat) On Sun, Feb 14, 2010 at 3:19 PM, Dennis Murphy <djmu...@gmail.com> wrote: > Hi: > > Here's one approach: the idea is to essentially stack the responses in both > data sets, create a factor that identifies the stacked variables, merges > the data > together and then combines 'variable' and 'grp' from the merged data frame > into a single factor for use as the groups = element in xyplot. (Got all > that?) > > library(lattice) > library(reshape) > # use the melt function of reshape to stack the responses and create an > # identifier (variable) for them. The responses themselves are put into a > # variable named 'value'. id.vars are kept separate, the others are stacked > # into a variable identifier called 'variable' and the vector of values > called > # 'values'... > Dat <- melt(dat, id.vars = c('x', 'grp')) > Dat2 <- melt(dat2, id.vars = c('x', 'grp')) > Dat3 <- rbind(Dat, Dat2) # concatenate the two data frames > Dat3$gv <- with(Dat3, paste(variable, grp, sep = "")) # combine factors > > xyplot(value ~ x, data = Dat3, groups = gv, type = "l") > > There are more elegant ways to create the grouping factor, which you may > need if you intend to create a 'nice' legend, but at this level it works. > > HTH, > Dennis > > On Sun, Feb 14, 2010 at 12:44 PM, Pat Schmitz <pschm...@illinois.edu>wrote: > >> All >> >> I want to overlay two variables on the same plot following their >> appropriate >> grouping. I have attempted to use subscripting in panel with >> panel.xyplot, >> but I can't get the grouping to follow into the panel...here is an >> example... >> >> >> dat<-data.frame( >> y= log(1:10), >> y2=10:19, >> x=1:10, >> grp = as.factor(1) >> ) >> >> dat2<-data.frame( >> y= log(10:19), >> y2= 20:29, >> x=1:10, >> grp = as.factor(c(2)) >> ) >> >> dat<-rbind(dat, dat2) >> >> # Here I plot both response variables on y axis by the same x >> xyplot(y2 ~ x, dat, groups=grp, type="l") >> xyplot(y ~ x, dat, groups=grp, type="l") >> >> # I want to overlay both in the same plot to compare >> xyplot(y~ x, dat, >> groups = grp, >> ylim = c(0,40), >> panel=function(x,y,subscripts, groups,...){ >> panel.xyplot(x, y, type="l") >> panel.xyplot(dat$x[subscripts], dat$y2[subscripts],type="l") >> } >> ) >> >> >> Thanks >> >> -- >> Patrick Schmitz >> Graduate Student >> Plant Biology >> 1206 West Gregory Drive >> RM 1500 >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > -- Patrick Schmitz Graduate Student Plant Biology 1206 West Gregory Drive RM 1500 [[alternative HTML version deleted]] ______________________________________________ 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.