[R] ordering a boxplot

William Dunlap wdunlap at tibco.com
Sat Mar 21 19:13:01 CET 2015


You can use the reorder() function to reorder the grouping vector's
factor levels according to a function of the data in each group.  E.g.,
compare the following two plots:

   d <- data.frame(Response=cos(1:15), Group=rep(c("A","B","C"),c(6,5,4)))
   par(mfrow=c(1,2))
   boxplot(Response ~ Group, data=d)
   boxplot(Response ~ reorder(Group, X=Response, FUN=median), data=d)


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Mar 20, 2015 at 2:20 PM, Antonio Silva <aolinto.lst at gmail.com>
wrote:

> Hello
>
> I'm using a dataframe (mydata) where row names are sampling points and
> column names are species in a multivariate analysis.
>
> If I write boxplot(mydata) I'll have boxplots for each species abundance in
> alphabetical order.
>
> How to get the boxes orderer by the median?
>
> Usually for this I write
>
> boxplot(value~category,at=rank(tapply(mydata$value,mydata$category,median)))
> but for this to work I need a column for the categories and another column
> for the values.
>
> Thanks in advance, best regards.
>
> Antonio Olinto
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list