Skip to content
Prev 256387 / 398502 Next

Adding text labels to lattice plots with multiple panels

On 2011-04-10 04:50, Jeff Stevens wrote:
Good point; I hadn't thought of that. Now I realize that a
solution that fiddles with the labels inside the panel
function is likely to be less efficient anyway. So let's
adjust the labels to be printed before we do the bwplot()
call. What's needed is to set to blanks the labels that
are duplicates because they correspond to duplicated
(f1,f2) combinations.

## add an adjusted labels variable to the data;
## lab is the current vector of labels;
  names(df)
  #[1] "f1"   "f2"   "dv"   "lab"

  df <- transform(df,
         lab2 = ifelse(duplicated(df[, c("f1","f2")]), "", lab))

## now use lab2 in bwplot()
  bwplot(dv ~ f1 | f2, data = df, ylim = c(0.5, 1),
   panel = function(x, y, ..., subscripts) {
     lab <- df$lab2[subscripts]   # note the lab2
     panel.bwplot(x, y,  ...)
     panel.text(x, 0.55, labels = lab)
   }
  )

Peter Ehlers