Pat Schmitz
2010-Feb-14 20:44 UTC
[R] xyplot, overlay two variables on one plot with group factors
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]]
Pat Schmitz
2010-Feb-14 21:54 UTC
[R] xyplot, overlay two variables on one plot with group factors
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 <djmuser@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 <pschmitz@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]]
David Winsemius
2010-Feb-14 22:15 UTC
[R] xyplot, overlay two variables on one plot with group factors
On Feb 14, 2010, at 3:44 PM, Pat Schmitz 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") > } > ) >My reading of the xyplot help page made me think that "subscripts" was logical and not an indexing variable, although the opposite is suggested by panel.superpose's help page. (Just my continued confusion about how to use lattice functions properly.) It sounds as though you are trying to superimpose what would otherwise be separate panels. So I'm never quite sure that when I mess around with panel arguments that I'm doing it right, but this produces what I think you are trying to do: xyplot(y~ x, dat, groups = grp, ylim = c(0,40), panel=function(x,y, groups,...){ panel.xyplot(x, y, groups, type="l", ...) panel.superpose(x, dat$y2, groups, type="l", ...) }) My hypothesis after experimenting with trying to leave out the , ... argument is that it is necessary to accept the subscripts argument, even though you don't actually need to do anything with it. I think that is implied by the fact that "subscripts (with no default) appears before the ellipsis in the arguments list.> > Thanks > > -- > Patrick Schmitz > Graduate Student > Plant Biology > 1206 West Gregory Drive > RM 1500 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT