[R] lattice dotplot with missing levels in factor variable

Boris.Vasiliev at forces.gc.ca Boris.Vasiliev at forces.gc.ca
Thu Jan 14 18:25:43 CET 2010


Hi,

I am trying to create a dotplot where each panel shows levels vs.
responses; the levels are sorted by responses but levels vary from one
panel to another.  However, I run into problems with controlling the
y-limits and y-labels.

In particular, suppose I have a data frame

rsp <- c(10,2,4,0,2,3)
lvl <-
factor(c("L1","L2","L3","L2","L3","L4"),levels=c("L1","L2","L3","L4"))
cat <- factor(c("A","A","A","B","B","B"),levels=c("A","B"))
xx <- data.frame(cat,lvl,rsp)
print(xx)

  cat lvl rsp
1   A  L1  10
2   A  L2   2
3   A  L3   4
4   B  L2   0
5   B  L3   2
6   B  L4   3

I tried to follow the "Lattice ..." book and used

dotplot(lvl~rsp|cat,data=xx,type=c("h","p"),
	    panel=function(x,y,...){y1 <- reorder(y,x)
                                  y1 <- y1[,drop=TRUE]
                                  panel.dotplot(x,y1,...)},
          prepanel=function(x,y){y1 <- reorder(y,x)
                                 y1 <- y1[,drop=TRUE]
                                 return(list(ylim=levels(y1)))},
          layout=c(1,2))

However, the resulting has incorrect y-labels:

Desired Plot      Achieved Plot

Panel B:          Panel B:

                  L4                  
L4 ----o          L1 ----o
L3 ---o           L3 ---o
L2 o              L3 o

panel A:          Panel A:

                  L4 
L1 ----------o    L1 ----------o
L3 ----o          L3 ----o
L2 --o            L2 --o

It appears that the y-limits and y-labels specified by the pre-panel
function are overwritten by something in dotplot.  Can anybody suggest
what I am doing incorrectly?

Regards,
Boris.



More information about the R-help mailing list