Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort boxplot by mean (and not median) in R

I have a simple boxplot, showing the distribution of a score for factor TYPE:

myDataFrame = data.frame( TYPE=c("a","a","b","b","c","c"), SCORE=c(1,1,2,3,2,1) )
boxplot( SCORE~TYPE, data=myDataFrame )

The various types are shown in the order they have in the data frame. I'd like to sort the boxplot by the mean of SCORE in each TYPE (in the example above, the order should be a,c,b).

Any hint?

like image 685
Mulone Avatar asked Mar 16 '12 17:03

Mulone


People also ask

How do I rearrange the order of a Boxplot in R?

To reorder the boxplot we will use reorder() function of ggplot2. By default, ggplot2 orders the groups in alphabetical order.

How do you show a mean in a Boxplot in R?

R. Output: In order to show mean values in boxplot using ggplot2, we use the stat_summary() function to compute new summary statistics and add them to the plot. We use stat_summary() function with ggplot() function.

Does Boxplot use median or mean?

The box covers the interquartile interval, where 50% of the data is found. The vertical line that split the box in two is the median. Sometimes, the mean is also indicated by a dot or a cross on the box plot.

Does R Boxplot show mean or median?

A boxplot summarizes the distribution of a continuous variable and notably displays the median of each group. This post explains how to add the value of the mean for each group with ggplot2.


1 Answers

This is a job for reorder():

myDataFrame$TYPE <- with(myDataFrame, reorder(TYPE, SCORE, mean))
boxplot( SCORE~TYPE, data=myDataFrame )

enter image description here

like image 102
Josh O'Brien Avatar answered Oct 18 '22 03:10

Josh O'Brien