title: Control the fill order and groups for a ggplot2 geom_bar
body: <pre><code>library(ggplot2)
  
  
   data &lt;- 
    data.frame(
      group=factor(c("a","c","b","b","c","a")),
      x=c("A","B","C", "D","E","F"),
      y=c(3,2,10,11,4,5)) 
  
  &gt; data
    group x  y
  1     a A  3
  2     c B  2
  3     b C 10
  4     b D 11
  5     c E  4
  6     a F  5
  
  #And plot this:
  ggplot(data)+
    geom_bar(aes(x=x, y=y, fill=group, order=group),
             stat="identity",
             position="dodge")+
    coord_flip()
  </code></pre>
  
  <p>This gives a figure where x is plotted according to factor levels:
  <img src="https://i.stack.imgur.com/onZKt.png" alt="enter image description here"></p>
  
  <p>But how can one reorder x according to a custom order of the <code>group</code> variable and at the same time arrange within <code>group</code> according to say descending <code>y</code>. For instance if I want to plot first "c" (red), then "a" (green) and then "b" (blue) groups, the plot order of the x-axis (<code>x</code> variable) would be: E, B, F, A, D, C. Note this may have resemblance to <a href="https://stackoverflow.com/questions/15251816/how-do-you-order-the-fill-colours-within-ggplot2-geom-bar">this</a> SO question.</p>