[R] lattice: aligning independent graphs

Sebastian P. Luque spluque at gmail.com
Thu May 31 07:26:00 CEST 2007

On Sun, 27 May 2007 15:09:43 -0700,
"Deepayan Sarkar" <deepayan.sarkar at gmail.com> 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"):


I'm not yet savvy with grid, but if one were to do the same for y axis
labels, I thought the following modification to your code might do that:

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

xyplot(1:9 ~ 1:9 | gl(3, 1, 9), layout=c(1, 3),
       ylab=myXlabGrob('Trial number', 'Subject number',
           'Experimental condition'), strip=FALSE)
---<---------------cut here---------------end---------------->---

which gives wrong width and placement.  How can this be modified so it
places the labels close to the axis annotation, centered on each panel?
Thanks in advance.



More information about the R-help mailing list