Last Updated
Viewed 1,594 Times

# Sorting a ggplot2 boxplot [duplicate]

Possible Duplicate:
Order Bars in ggplot2 bar graph

I have the following data frame (`df`) in R that I wish to create a box plot for:

``````    Entity  Mean    Min Q1      Med     Q3      Max
1   ABCD    88.65   0   75.0    100.0   100.0   100
2   BAAF    88.73   0   91.0    100.0   100.0   100
3   BCC4    70.26   0   20.0    100.0   100.0   100
4   C9FE    77.98   0   66.0    86.0    100.0   100
5   D3AA    71.97   0   51.0    82.0    100.0   100
``````

The code I use is as follows:

``````Lplot <- ggplot(df, aes(Entity, ymin=Min, lower=Q1, middle=Med, upper=Q3, ymax=Max,
fill=Mean))
mth <- theme(axis.text.x = element_text(angle = 90, hjust = 1))
bplot <- geom_boxplot(stat="identity")
sfg <- scale_fill_gradient("Mean", low="green", high="red")

Lplot + mth + sfg + bplot
``````

This graphs the data frame with `Entity` on the x-axis and the appropriate quantiles on the y-axis. However, I would like to sort the plots by `Mean`. I can achieve this effect by replacing `aes(Entity,...` with `aes(factor(Mean),...` but then the mean values appear on the x-axis when I still want the `Entity` names there.

Essentially what I am after is a boxplot sorted by `Mean` but labelled by `Entity`. How can I achieve this?

# Similar Question 1 : Order boxplot at higher level (R, ggplot2)

I am using the following code:

`````` library(ggplot2)
mtcars\$carb <- as.factor(mtcars\$carb)
mtcars\$am <- as.factor(mtcars\$am)
sort_table <- data.frame("carb" = c(1,2,3,4,6,8), "class" = c("class A", "class B", "class A", "class C", "class B", "class A"))
ggplot(mtcars) +
geom_boxplot(aes(x = carb, y = mpg, fill = am),
position = position_dodge(0.9))
``````

This results in this plot:

How can I sort the boxplot at a higher level, i.e., by class, as defined in the sort_table data.frame? And how can I add labels that indicate each class? The desired result looks like this:

Please note that I need to apply this to a larger dataset, so I need to sort using the sort_table data.frame and not by "manual" ordering using something like `c(1,3,8,2,6,4)`. Thank you.

# Similar Question 2 : Order of boxplot in ggplot2 [duplicate]

This question already has an answer here:

here is my code:

``````> hdata\$Mono<- factor(hdata\$Mon)

> ggplot(hdata, aes(y= Test1, x= Mono))+ geom_boxplot()
``````

And this is what it is giving:

I want to format the x axis in a way so that it will start from 12 and go like this 12, 1, 2, 3, 4, 5, 6 ,7, 8

# Similar Question 3 : How to plot a comparison of boxplots in R using ggplot2

I have a data frame with three variables (n, Parametric, Mean) in column format. n takes on values 25, 50, or 100. Parametric takes on either "Yes" or "No". Mean takes on a numerical value. Similar to the very simplified version here:

``````n     Parametric     Mean
25       Yes          1.2
25       No           1.5
50       Yes          0.9
50       No           1.1
100      Yes          1.0
100      No           1.2
``````

I would like to make a boxplot comparing the Parametric mean values (mean values that have parametric=yes) vs. Non parametric mean values (mean values that have parametric=no) for each of the three different n values.

Except I want my legend to be Parametric: Yes or No, the x-values to be n, and the y values to be the mean values.

The code:

``````    # Create boxplot comparisons
ggplot(dataMean, aes(x=n, y=Mean, color=Parametric))+
geom_boxplot()
``````

is only giving me something that has two boxplots, one for Parametric=yes and one for Parametric=no. I am looking for a plot that compares parametric=yes vs parametric=no for each n value. So ultimately I want 6 boxplots (2 boxplots for each n value), color coordinated by their parametric column.

How should I organize my data in order to be able to achieve this result? And what code would then create a plot with these 6 boxplots?

cc