title: How do you order stacked bar categories by size in ggplot?
body: <p>I am trying to reorder categories in a ggplot bar chart by the size of the variables, with the biggest categories first, followed by increasingly minor ones. I've tried playing with the order in a few ways but only seem to be able to set the order manually which isn't very helpful for big data sets.</p>
  
  <p>For example:
  Let's say I have the following data:</p>
  
  <pre><code>mydata &lt;- read.table(header=TRUE, text="
  shop    fruit   varieties   km_away
  shop1   apple   0   12
                       shop1  banana  0   12
                       shop1  pear    2   12
                       shop1  melon   1   12
                       shop1  orange  1   12
                       shop1  peach   3   12
                       shop1  apricot 6   12
                       shop1  lime    1   12
                       shop2  apple   1   1
                       shop2  banana  0   1
                       shop2  pear    2   1
                       shop2  melon   2   1
                       shop2  orange  5   1
                       shop2  peach   4   1
                       shop2  apricot 11  1
                       shop2  lime    0   1
                       shop3  apple   0   2
                       shop3  banana  1   2
                       shop3  pear    2   2
                       shop3  melon   1   2
                       shop3  orange  4   2
                       shop3  peach   1   2
                       shop3  apricot 11  2
                       shop3  lime    1   2
                       shop4  apple   0   5
                       shop4  banana  0   5
                       shop4  pear    3   5
                       shop4  melon   2   5
                       shop4  orange  6   5
                       shop4  peach   1   5
                       shop4  apricot 9   5
                       shop4  lime    0   5
                       ")
  </code></pre>
  
  <p>I can usefully visualise it like this:</p>
  
  <pre><code>library(ggplot2)
  library(RColorBrewer)
  p &lt;- ggplot(data = mydata, aes(x=reorder(shop, km_away), y=varieties, fill=fruit))+
    geom_bar(stat="identity") + coord_flip()+scale_fill_brewer(palette="Accent")
  p
  </code></pre>
  
  <p>But how do I tell ggplot to plot the apricots first, then the oranges, etc? This will make it much easier to visually compare different categories across shops. </p>