[R] boxplot - how to supress groups with low counts

Peter Ehlers ehlers at ucalgary.ca
Fri Jan 28 15:21:05 CET 2011


On 2011-01-28 06:09, Uwe Ligges wrote:
>
>
> On 28.01.2011 14:21, pdb wrote:
>>
>> In a boxplot - how can I prevent groups where the number of cases is less
>> than a set threshold from being plotted.
>>
>> set.seed(42)
>>    DF<- data.frame(type=sample(LETTERS[1:5], 100, replace=TRUE),
>> cost=rnorm(100))
>>    count<- boxplot(cost ~ type, data=DF, plot = 0)
>>    count$n
>>
>> ## how to only include plots where count$n>   18
>>    boxplot(cost ~ type, data=DF)
>>
>> Thanks in advance for any solutions.
>
> set.seed(42)
> DF<- data.frame(type = sample(LETTERS[1:5], 100, replace=TRUE),
>                    cost = rnorm(100))
> count<- boxplot(cost ~ type, data=DF, plot = 0)
> for(i in c(1,3)) count[[i]]<- count[[i]][,count$n>  18]
> for(i in c(6,2)) count[[i]]<- count[[i]][count$n>  18]
> bxp(count)
>
> Uwe Ligges
>

Here's another way:

  count <- c(table(DF$type))
  DF1 <- subset(DF, type %in% levels(type)[count > 18])
  boxplot(cost ~ type, DF1)
  boxplot(cost ~ type, droplevels(DF1))

Peter Ehlers



More information about the R-help mailing list