How to label panels in lattice

matteo picture matteo · Dec 14, 2011 · Viewed 13.3k times · Source

here is a simple problem that surely you already come across, but is giving me a big headache...

I have a dataframe like this:

set.seed(3)
mydata <- data.frame(var = rnorm(100,20,1),
                     temp = sin(sort(rep(c(1:10),10))),
                     subj = as.factor(rep(c(1:10),5)))

and I need to make a scatter plot for each subj, not a problem, but... what i want is to replace the strips from the lattice and add a label to each plot. I manage to do this with the following code, but I'm still not happy...

xyplot(var ~ temp | subj, 
       data = mydata,
       strip=FALSE,
       panel = function(x, y,...) {
               panel.xyplot(x, y,...)
               panel.text(1,21,labels=which.packet())
               }) 

The last bit...where i got stacked...is how to print letters instead of numbers in each panel. I would like to call the panels a,b,c... and so on.

Any suggestions... Many thanks matteo

Answer

Oscar Perpi&#241;&#225;n picture Oscar Perpiñán · Dec 14, 2011

You almost got it. If you need letters, then index letters with panel.number():

xyplot(var ~ temp | subj, 
   data = mydata,
   strip=FALSE,
   panel = function(x, y,...) {
           panel.xyplot(x, y,...)
           panel.text(1,21,labels=letters[panel.number()])
           }) 

You can also define another character vector inside your panel function and use this indexing schema with it.