title: How to order stack bar based on cumulative value in each group
body: <p>I'm trying to plot a geom_bar with the stack order depending on the cumulative value of each group in each bar. therefore, each bar should have a different order. Here is a reproducible example</p>
  <pre><code>set.seed(8)
  
  #create dataframe
  name &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;i&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;, &quot;aa&quot;)
  group &lt;- c(&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;2&quot;,&quot;2&quot;, &quot;2&quot;, &quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;2&quot;)
  subgroup &lt;- c(&quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;)
  value &lt;- runif(26, min = 0, max = 10)
  
  df &lt;- data.frame(name, group, subgroup,value)
  
  #plot
  ggplot(data=df, aes(x=group, y=value)) + 
    geom_bar(stat=&quot;identity&quot;,aes(fill=reorder(subgroup, value)))
  </code></pre>
  <p>The latter results in the following plot:
  <a href="https://i.stack.imgur.com/MlXRV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MlXRV.png" alt="enter image description here" /></a></p>
  <p>Note that it is clear that the B subgroup represents a higher value than C in group 1, but it is plotted above C. I want that, for each group, the subgroups are ordered based on their cumulative value in the group. Does anybody know how to achieve this? Thanks in advance!</p>