[R] lattice

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 30 17:11:21 CEST 2001


On Thu, 30 Aug 2001, R. Heberto Ghezzo wrote:

>    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.

You can't (easily) mix grid and traditional graphics calls.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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