title: Sorting a ggplot2 boxplot
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph">Order Bars in ggplot2 bar graph</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following data frame (<code>df</code>) in R that I wish to create a box plot for:</p>
  
  <pre><code>    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   
  </code></pre>
  
  <p>The code I use is as follows:</p>
  
  <pre><code>Lplot &lt;- ggplot(df, aes(Entity, ymin=Min, lower=Q1, middle=Med, upper=Q3, ymax=Max,
           fill=Mean))
  mth &lt;- theme(axis.text.x = element_text(angle = 90, hjust = 1))
  bplot &lt;- geom_boxplot(stat="identity")
  sfg &lt;- scale_fill_gradient("Mean", low="green", high="red")
  
  Lplot + mth + sfg + bplot
  </code></pre>
  
  <p>This graphs the data frame with <code>Entity</code> on the x-axis and the appropriate quantiles on the y-axis. However, I would like to sort the plots by <code>Mean</code>.
  I can achieve this effect by replacing <code>aes(Entity,...</code> with <code>aes(factor(Mean),...</code> but then the mean values appear on the x-axis when I still want the <code>Entity</code> names there.</p>
  
  <p>Essentially what I am after is a boxplot sorted by <code>Mean</code> but labelled by <code>Entity</code>. How can I achieve this?</p>