[R] ordering a boxplot
Boris Steipe
boris.steipe at utoronto.ca
Sun Mar 22 01:09:55 CET 2015
... just for completeness - the more concise way: (no need to go through names()).
boxplot(mydata[,order(apply(mydata,2,median))])
... or descending
boxplot(mydata[,order(-apply(mydata,2,median))])
B.
On Mar 21, 2015, at 7:04 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
> There may be more concise ways to do this - but you are 99% there with your approach:
> try:
>
> boxplot(mydata[,names(sort(apply(mydata,2,median)))])
>
> B.
>
>
> On Mar 21, 2015, at 6:49 PM, Antonio Silva <aolinto.lst at gmail.com> wrote:
>
>> Thanks Bill and David
>>
>> Here goes an example
>>
>> SP1<-c(9,6,7,8,5,8,7,5,9,7)
>> SP2<-c(1,3,4,2,4,2,5,3,2,1)
>> SP3<-c(4,6,7,5,7,8,7,6,5,4)
>> SP4<-c(5,4,3,5,2,3,4,3,4,2)
>> mydata<-data.frame(SP1,SP2,SP3,SP4)
>> rownames(mydata)<-c("ST1","ST2","ST3","ST4","ST5","ST6","ST7","ST8","ST9","ST10")
>> mydata
>> boxplot(mydata)
>>
>> Note that this data frame does not have the format Response ~ Group. In my
>> real matrix I have up to 40 species.
>>
>> Is there any way to have the species ordered by their median abundance (or
>> other parameter?)
>>
>> The desired order is given by names(sort(apply(mydata,2,median)))
>>
>> Thanks once more,
>>
>> Antonio Olinto
>> Fisheries Institute
>> Sao Paulo, Brazil
>>
>> 2015-03-21 15:13 GMT-03:00 William Dunlap <wdunlap at tibco.com>:
>>
>>> 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]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list