R: Lattice graphics for multilevel data

Some examples using lattice R to visualize the Bryk and Raudenbush High School Math Achievement data

ADD: USING panel.subgroups from 'fun'

```   ##
##  Using mixed models with R
##  From: BeyondRepeated-2007-02-22-UsingRforGraphs.R
##
```
```   library(nlme)
library(lattice)
```
```   source("http://www.math.yorku.ca/~georges/R/fun.R")
source("http://www.math.yorku.ca/~georges/R/Plot3D.R")
```
```   # partial data set

hs
names(hs)
summary(hs)
hs\$ses         #  (data frame) \$ (variable name)
xqplot(hs)

#
#  lattice (or 'trellis') graphics
#
#

library(lattice)

xyplot( mathach ~ ses | school, hs)                  # y ~ x | panel
xyplot( mathach ~ ses | school, hs, groups = female)    # y ~ x | panel  ... groups in panels
xyplot( mathach ~ ses | school, hs, groups = female, auto.key = T)    # y ~ x | panel  ... groups in panels

hs\$SchoolID <- factor(paste(
ifelse( hs\$sector == 1, "C", "P"), hs\$school, sep="") )

xyplot( mathach ~ ses | SchoolID, hs, groups = female, auto.key = T)
```
```   td( col = c('red','blue'))  # change colors

hs\$SchoolID <- reorder( hs\$SchoolID, hs\$mathach+ 1000*hs\$sector)
xyplot( mathach ~ ses | SchoolID, hs, groups = female, auto.key = T)
```
```   hs\$Sex <- factor( ifelse( hs\$female == 1, "Female", "Male"))

xyplot( mathach ~ ses | SchoolID, hs, groups = Sex, auto.key = list(columns=2))
```
```   xyplot( mathach ~ ses | SchoolID, hs, groups = Sex, auto.key = list(columns=2),
panel = panel.superpose,
panel.groups = function( x, y, ...) {
panel.xyplot(x, y, ...)
panel.lmline(x, y, ...)
})

# Show patterns within sectors
```
```   hs\$Sector <- factor( ifelse( hs\$sector == 1, "Catholic", "Public"))
```
```   xyplot( mathach ~ ses |  Sector, hs, groups = SchoolID,
panel = panel.superpose,
panel.groups = function( x, y, ...) {
panel.lmline(x, y, ...)
})
```
```   # Show patterns within sectors and Sex
```
```   xyplot( mathach ~ ses |  Sector * Sex, hs, groups = SchoolID,
panel = panel.superpose,
panel.groups = function( x, y, ...) {
panel.lmline(x, y, ...)
})
```

```   xyplot( mathach ~ ses |  Sector * Sex, hs, groups = SchoolID,
panel = panel.superpose,
panel.groups = function( x, y, ...) {
panel.loess(x, y, ...)
})
```