[R] An example of the boxplot thickness problem

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Nov 12 06:21:15 CET 2008


On 11/11/08, Kenneth Roy Cabrera Torres <krcabrer at une.net.co> wrote:
> Hi R users:
>
>  I reproduce the problem that I have with the
>  boxplot thickness:
>  ------------------------------------------------------------------
>  # A data frame:
>  set.seed(123)
>  cont1<-c(rnorm(10,1),rnorm(5,3),rnorm(12,5),rnorm(14,3),rnorm(4,5))
>  categ1<-factor(c(rep("A",10+5+12),rep("B",14+4)))
>  categ2<-c(rep("Z",10),rep("Y",5),rep("X",12),rep("Y",14),rep("X",4))
>
>  data1<-data.frame(cont1,categ1,categ2)
>
>  # This is the variable that I want that each boxplot
>  # be thickness proportional. (could be any other, the only
>  # condition is that I have a number (or NA) for each combination of the
>  # two categorical variables).
>  cont2<-tapply(cont1,list(categ1,categ2),length)/length(cont1)
>
>  require(lattice)
>  # This is the standard boxplot
>  bwplot(categ2~cont1|categ1,data=data1)
>  # I try:
>  bwplot(categ2~cont1|categ1,box.ratio=cont2,data=data1)
>  # This one also
>  bwplot(categ2~cont1|categ1,box.ratio=t(cont2),data=data1)
>  ----------------------------------------------------------------
>
>  Problems:
>  1. I expect that the boxplot for the B and Y combination
>    would be the most thick, and in second place the A and X
>    and the last the X and B combination.

I think you want something like

bwplot(categ2 ~ cont1 | categ1, data=data1,
       panel = function(..., box.width) {
           panel.bwplot(...,
                        box.width = as.numeric(cont2[packet.number(), ]))
       })

>  2. Why the other lines in the box of the boxplot?

You are providing 6 width values for 3 boxes in each panel; the values
are being recycled and each box is being drawn twice, with different
widths.

-Deepayan



More information about the R-help mailing list