[R] How to make a boxplot with exclusion of certain groups

Chuck Cleland ccleland at optonline.net
Mon Apr 19 18:43:11 CEST 2010


On 4/19/2010 12:21 PM, Josef.Kardos at phila.gov wrote:
> This seems like a simple thing, but I have been stuck for some time.  My 
> data has 2 columns.  Column 1 is the value, and column 2 is the Site where 
> data was collected.  Column 2 contains 7 different Sites (i.e. groups).  I 
> am only interested in showing 3 groups on a single boxplot. 
> 
> I have tried various methods of subsetting the data, in order to only have 
> the 3 groups in my subset.  However even after doing this, all 7 groups 
> carry forward, so that when I make a boxplot of my subsetted data, all 7 
> groups still appear in the x-axis labels; all 7 groups also appear in the 
> boxplot summary (i.e. the values returned with boxplot (…plot=FALSE)  ) . 
> Even if I delete the unwanted groups from the ‘levels’ of Column 2, they 
> still appear on the plot, and in the boxplot summary statistics.
> 
> There are various tricks I can do with the boxplot summary statistics to 
> correct for this, but they get complicated when I want to change the 
> algorithm for calculating outliers and their corresponding groups. Rather 
> than do all these tricks, it seems much simpler to fully exclude the 
> unwanted groups from the beginning.  But this doesn’t appear to work
> 
> Any ideas?

library(gdata) # for drop.levels()

DF <- data.frame(site = rep(LETTERS[1:7], each=20), y = runif(7*20))

boxplot(y ~ drop.levels(site), data=subset(DF, site %in% c('A','D','F'),
drop=TRUE))

> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list