[R] lattice

Paul Murrell p.murrell at auckland.ac.nz
Fri Aug 31 00:28:39 CEST 2001


Hi


> >    Hello. I know that lattice is still in beta beta but. . .
> > Nick Ellis wrote to S-news with an example of 'trellis' with several
> > lines in each panel.
> > df<-expand.grid(fac1=letters[1:2],x=seq(0,1,0.1),fac2=LETTERS[1:4])
> > df$y<-df$x*codes(df$fac1)+codes(df$fac2)*df$x^2+rnorm(nrow(df))/3
> > xyplot(y ~ x | fac2,
> >           groups=fac1,
> >          data=df,
> >          panel=function(x,y,subscripts,groups){
> >               panel.superpose(x,y,subscripts,groups)
> >              for(g in as.character(unique(groups[subscripts]))) {
> >                  sub <- groups[subscripts] == g
> >                  fit  <- lm(y~x+I(x^2),subset=sub)
> >                  ord <- order(x[sub])
> >                 lines(x[sub][ord],fitted(fit)[ord])
> >             }
> >          }
> >   )
> > if I run this in R 1.3 after library(grid), library(lattice) I get the 4
> > panels with the 2 sets of points in different colors in each panel, but
> > not the panel function, i.e. the fitted quadratics, 2 per panel.  Is
> > this because lattice is still beta? or the construccion of the panel
> > function is different in lattice?
>
> lattice != trellis, being built on grid.  Try
>
> xyplot(y ~ x | fac2,
>           groups=fac1, data=df,
>           panel=function(x,y,subscripts,groups){
>               panel.superpose(x,y,subscripts,groups)
>               for(g in as.character(unique(groups[subscripts]))) {
>                  sub <- groups[subscripts] == g
>                  fit  <- lm(y~x+I(x^2),subset=sub)
>                  ord <- order(x[sub])
>                  grid.lines(x[sub][ord],fitted(fit)[ord],
>                            default.units = "native")
>             }
>          }
>       )
>
>
> Not quite the same, as strips are done differently too.


To get the strips the same as well, try ...

xyplot(y ~ x | fac2,
          strip=function(...) strip.default(style=3, ...),
          groups=fac1, data=df,
          panel=function(x,y,subscripts,groups){
              panel.superpose(x,y,subscripts,groups)
              for(g in as.character(unique(groups[subscripts]))) {
                 sub <- groups[subscripts] == g
                 fit  <- lm(y~x+I(x^2),subset=sub)
                 ord <- order(x[sub])
                 grid.lines(x[sub][ord],fitted(fit)[ord],
                           default.units = "native")
            }
         }
      )

Paul


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list