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

Jeff Stevens stev0175 at gmail.com
Tue Apr 12 15:18:37 CEST 2011


Hi Deepayan

Many thanks for bringing the which.packet() function to my
attention--I missed it in the book.  I do prefer that the panel
function applies the labels to the data rather than adding them
directly to the data in advance.

Jeff


On Tue, Apr 12, 2011 at 8:50 AM, Deepayan Sarkar
<deepayan.sarkar at gmail.com> wrote:
> 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