title: Sort stacked bar plot by cumulative value in R
body: <p>I am pretty new to R and i'm trying to get a stacked bar plot. My data looks like this:</p>
  
  <pre><code>    name    value1  value2
  1   A       1118    239
  2   B       647     31
  3   C       316     1275
  4   D       2064    230
  5   E       231     85
  </code></pre>
  
  <p>I need a horizontal bar graph with stacked values, this is as far as i can get with my limited R skills (and most of that is also copy-pasted): </p>
  
  <pre><code>melted &lt;- melt(data, id.vars=c("name"))
  
  melted$name &lt;- factor(
    melted$name, 
    levels=rev(sort(unique(melted$name))), 
    ordered=TRUE
  )
  
  melted2 &lt;- melted[order(melted$value),]
  
  ggplot(melted2, aes(x= name, y = value, fill = variable)) + 
    geom_bar(stat = "identity") +
    coord_flip()
  </code></pre>
  
  <p>It even took me several hours to get to this point, with witch I am pretty content as far as looks go, this is the produced output</p>
  
  <p><img src="https://i.stack.imgur.com/yGQH2.jpg" alt="enter image description here"></p>
  
  <p>What I now want to do is to get the bars ordered by summed up value (D is first, followed by C, A, B, E). I googled and tried some <code>reorder</code> and <code>order</code> stuff, but I simply can't get it to behave like I want it to. I'm sure the solution has to be pretty simple, so I hope you guys can help me with this.</p>
  
  <p>Thanks in advance!</p>