[R] dotplot as a background for multiple barchart plots (with Lattice)

Christian Charles christian.charles at inrialpes.fr
Thu Apr 7 15:46:41 CEST 2011


Hi R users,

I'm kind stuck in my R experience...
I want to have a multiple barchart with n windows, but with the same 
background for each window, and I want that background to be a recorded 
dotplot.
First, is it possible ?
If so, I guess I have to call a record plot in the panel function but how ?
As there is no panel.plot and I can't use the recorded plot as an 
argument for the panel.dotplot() call, I'm stuck.
I think I close but I need some help.

Many thanks and regards,

Christian


### dummy data building
test <- data.frame(Subject=rep(c("A", "B", "C", "D"), each=6),
                    Type=rep(c("EEE", "FFF", "GGG", "HHH", "III", 
"JJJ"), times=4),
                    MeanX=abs(rnorm(24)),
                    MeanZ=abs(rnorm(24)))

### data for regrouping all means
all <- data.frame(Type=levels(factor(test$Type)), MeanX = c(0), MeanZ = 
c(0))

### building means...
for(s in levels(factor(test$Subject)))
{
     tmp <- data.frame(Type = all$Type)
     for(t in tmp$Type)
     {
             tmp$MeanX[tmp$Type == t] <- mean(test$MeanX[test$Type==t & 
test$Subject==s])
             tmp$MeanZ[tmp$Type == t] <- mean(test$MeanZ[test$Type==t & 
test$Subject==s])
     }
     all$MeanX = all$MeanX + tmp$MeanX
     all$MeanZ = all$MeanZ + tmp$MeanZ
}
l <- length(levels(factor(test$Subject)))
all$MeanX = all$MeanX/l
all$MeanX = all$MeanZ/l

### plot for means
meanplot <- dotplot(MeanX+MeanZ ~ Type, data=all, cex = 1.2, xlab="", 
ylab="",
     panel = panel.superpose,
     panel.groups = function(x, ..., group.number){
     panel.dotplot(x + (group.number - 1.5)/3, ... )
     })

### test plot in order to see if there is something wrong.
plot(meanplot)

### final plot
barchart(    MeanX + MeanZ ~ Type|Subject,
          data=test ,
          scales = list(x = list(rot = 60)),
          layout=c(1, l),
          panel = function(...) {
             panel.superpose
             #panel.dotplot(meanplot) # so this is want I want, my 
recorded dotplot as a background...
             panel.abline(h=0)
             panel.grid(v = -length(levels(factor(test$Type))), h=0)
              panel.barchart(...)
             })


BTW, this R code might not be perfect and surely, there are better ways 
to build my data frames, but that's my newbie way to do it.



More information about the R-help mailing list