R lattice Labels for bwplots

635 Views Asked by At

I would like to label some of the statistic on each box and whisker in a lattice bwplot. A generic example below.

#---Some dummy data
Rock<-c("Rock1","Rock2","Rock3")
Zone<-as.data.frame(c("Zone10","Zone11","Zone12"))
Domain<-as.data.frame(c("Domain1","Domain2"))
Dt <- as.data.frame(rnorm(100))
Dt<-merge(Dt,Zone)
Dt<-merge(Dt,Rock)
Dt<-merge(Dt,Domain)
names(Dt)<-c("Data","Zone","Rock","Domain")

#--- Use aggregate to get the number of values for each combination of three factors (100 each)
aggregate(Data~Rock*Zone*Domain,Dt,FUN=length)

require(lattice)
#--- create a lattice plot and attempt to label the number of value associated with each BnW
bwplot(Rock~Data|Zone*Domain,
    data=Dt,
    xlim=c(-5,5),
       panel=function(...){
        panel.bwplot(...)
        panel.text(-4,c(1,2,3),length(x))
    }

)

This is not working - not sure why I'm getting labels saying 45 instead of 100. There must be a way to access things like length, mean, median and so on for each box and whisker in each panel?

1

There are 1 best solutions below

1
Wave On

I think you have to calculate the values yourself, which is easy with for example the plyr package (although you can also keep working with aggregate).

library(plyr)
agg=ddply(Dt,c("Zone","Rock","Domain"),summarise,length=length(Data), max=max(Data),median=median(Data))  # max or median or ... to have the xvalues were you want to plot the values of length

require(lattice)
bwplot(Rock~Data|Zone*Domain,
   data=Dt,
   xlim=c(-5,5),
   panel=function(...){
     panel.bwplot(...)
     panel.text(agg$max+1,c(1,2,3),agg$length)  # plus one to not mask the last dot
   }
   )