[R] xyplot, overlay two variables on one plot with group factors

David Winsemius dwinsemius at comcast.net
Sun Feb 14 23:15:29 CET 2010


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



More information about the R-help mailing list