title: sort bar chart by sum of values in ggplot
body: <p>example data:</p>
  
  <pre><code>player &lt;- c("a", "b", "a", "b", "c", 
              "a", "a", "b", "c", "b", 
              "c", "a", "c", "c", "a")
  is.winner &lt;- c(TRUE, FALSE, TRUE, TRUE, TRUE, 
                 FALSE, TRUE, TRUE, TRUE, FALSE, 
                 TRUE, TRUE, TRUE, TRUE, FALSE)
  
  df &lt;- data.frame(player, is.winner)
  </code></pre>
  
  <p>my first graph looks something like this</p>
  
  <p><a href="https://i.stack.imgur.com/Sewm3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Sewm3.png" alt="enter image description here"></a></p>
  
  <pre><code>ggplot(data=df, aes(x=player, y=as.numeric(is.winner))) +
    geom_bar(stat="summary", fun.y=sum) + 
    coord_flip()
  </code></pre>
  
  <p>What I would like to do is sort the df$player axis by the sum of "TRUE" values, so that it looks something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/XNdXX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XNdXX.png" alt="enter image description here"></a></p>
  
  <p>I realize I could use something like this:</p>
  
  <pre><code>df$player &lt;- factor(df$player, levels=c("b", "a", "c"))
  </code></pre>
  
  <p>But the actual data has far more 'player names'. In addition I would like to something similar with win percentages, etc. So automatic sorting would be great. Example of win percentage below</p>
  
  <p><a href="https://i.stack.imgur.com/tav5p.png" rel="noreferrer"><img src="https://i.stack.imgur.com/tav5p.png" alt="enter image description here"></a></p>
  
  <pre><code>df$is.winner &lt;- factor(df$is.winner, levels=c("TRUE", "FALSE"))
  df$player &lt;- factor(df$player, levels=c("c", "b", "a"))
  
  library(scales)
  library(RColorBrewer)
  ggplot(data=df, aes(x=player)) +
    geom_bar(aes(fill=is.winner),position='fill')+
    scale_y_continuous(labels=percent)+
    scale_fill_brewer(palette="Set2") +
    coord_flip()
  </code></pre>