title: Sort barplot columns ggplot
body: <p>I have this R code code to generate the following barplot:</p>
  
  <pre><code>d &lt;- data.frame(case = c(1,2,3,4), 
                  var=c('foo', 'foo', 'foo', 'foo','bar', 'bar', 'bar', 'bar'), 
                  val=c(9,2,2,4,6,1,2,3))
  d$var &lt;- as.factor(d$var)
  d$case &lt;- as.factor(d$case)
  ggplot(d, aes(x = case, y = val, fill = var)) + scale_x_discrete() + geom_bar(position="dodge")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xwHab.png" alt="enter image description here"></p>
  
  <p>How can I sort the bars by the value of 'bar'?</p>
  
  <p>Disclaimer: this might be a duplicate of <a href="https://stackoverflow.com/questions/10746342/sorting-ggplot2-box-plot-by-2-columns">https://stackoverflow.com/questions/10746342/sorting-ggplot2-box-plot-by-2-columns</a> but this was not answered, so...</p>