Last Updated
Viewed 1,594 Times

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,
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?

I am using the following code:

 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: enter image description here

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:

enter image description here

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.

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:

enter image description here

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

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.

Similar to the image below:

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))+

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?

Similar Question 6 (2 solutions) : ggplot2 boxplot

Similar Question 7 (1 solutions) : Boxplot with ggplot2

Similar Question 8 (1 solutions) : Creating a boxplot, ggplot2

Similar Question 9 (3 solutions) : annotate boxplot in ggplot2