Search code examples
rggplot2lattice

What is the equivalent of facet_wrap in Lattice


Say we have some data like this:

dta <- data.frame(
   group = rep(letters[1:8], each=1000), 
   x = runif(8000), y=runif(8000)
)

I would like to produce a Lattice plot containing y ~ x for each group. But, with groups a-d on the first row, and e-h on the second row. i.e., I would like to do the equivalent of

 qplot(x=x, y=y, data=dta) + facet_wrap(~ group, nrow=2)

But in Lattice. What is the best way to do this?


Solution

  • library(lattice)
    xyplot(y ~ x | group, dta, layout=c(4,2), as.table = TRUE)
    

    As suggested by @BenBarnes, as.table = TRUE keeps the order of the facets.

    enter image description here