[R] Adding text labels to lattice plots with multiple panels

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Apr 12 08:50:54 CEST 2011


On Mon, Apr 11, 2011 at 12:49 AM, Jeff Stevens <stev0175 at gmail.com> wrote:
> Many thanks, Peter.  This works brilliantly, and I prefer to have the
> labels assigned outside of panel function as well.

You could also consider using which.packet(). You haven't explicitly
told us how the labels are matched with the boxplots, but assuming
that the labels are in the order of plotting, you can do [using the
original data]:


df <- data.frame(f1, f2, dv)
lab <- c(1, 2, 3, 4, 5, 6)

bwplot(dv ~ f1 | f2, data = df, ylim = c(0.5, 1),
       text.labels = as.character(lab),
       panel = function(x, y, ..., text.labels) {
           panel.bwplot(x, y, ...)
           n <- nlevels(x)
           i <- seq_len(n) + (which.packet() - 1) * n
           print(text.labels[i])
           panel.text(x = seq_len(n), y = 0.55,
                      labels = text.labels[i])
       })

-Deepayan



More information about the R-help mailing list