Skip to content
Prev 116638 / 398498 Next

lattice: aligning independent graphs

On 5/26/07, Zack Weinberg <zack at cogsci.ucsd.edu> wrote:
Better in what sense? You are trying to use lattice to do something
that lattice isn't designed to do, so there is no solution that is
"clean" in a philosophical sense. Ideally, you want something like
grid, that allows you to control the layout in as much detail as you
want (you can still use panel.xyplot, panel.grid, etc. as "panel
functions" once you set up everything else). On the other hand,
lattice gets you very close, and the only improvement I can think of
is some sort of automated scheme to align the x labels to the
respective columns. Here's one approach to do that (taking advantage
of the fact that xlab can be an arbitrary "grob"):


library(grid)

myXlabGrob <-
   function(...) ## ...is lab1, lab2, etc
{
   ## you can add arguments to textGrob for more control
   ## in the next line
   labs <- lapply(list(...), textGrob)
   nlabs <- length(labs)
   lab.heights <-
       lapply(labs,
              function(lab) unit(1, "grobheight", data = list(lab)))
   lab.layout <-
       grid.layout(ncol = nlabs, nrow = 1,
                   heights = do.call(max, lab.heights),
                   widths = unit(1, "null"),
                   respect = TRUE)
   lab.gf <- frameGrob(layout = lab.layout)
   for (i in seq_len(nlabs))
   {
       lab.gf <- placeGrob(lab.gf, labs[[i]], row = 1, col = i)
   }
   lab.gf
}


xyplot(1:9 ~ 1:9 | gl(3, 1, 9),
       layout = c(3, 1),
       xlab =
       myXlabGrob('Trial number',
                  'Subject number',
                  'Experimental condition'),
       strip = FALSE)

-Deepayan