title: ggplot2: create ordered group bar plot - (use reorder)
body: <p>I want to create grouped bar plot while keeping order. If it was single column and not a grouped bar plot use of reorder function is obvious. But not sure how to use it on a melted data.frame.</p>
  
  <p>Here is the detail explanation with code example:</p>
  
  <p>Lets say we have following data.frame:</p>
  
  <pre><code>d.nfl &lt;- data.frame(Team1=c("Vikings", "Chicago", "GreenBay", "Detroit"), Win=c(20, 13, 9, 12))
  </code></pre>
  
  <p>plotting a simple bar plot while flipping it.</p>
  
  <pre><code>ggplot(d.nfl, aes(x = Team1, y=Win)) + geom_bar(aes(fill=Team1), stat="identity") + coord_flip()
  </code></pre>
  
  <p>above plot will not have an order and if I want to order the plot by win I can do following:</p>
  
  <pre><code>d.nfl$orderedTeam &lt;- reorder(d.nfl$Team1, d.nfl$Win)
  ggplot(d.nfl, aes(x = orderedTeam, y=Win)) + geom_bar(aes(fill=orderedTeam), stat="identity") + coord_flip()
  </code></pre>
  
  <p>Now lets say we add another column (to original data frame)</p>
  
  <pre><code>d.nfl$points &lt;- c(12, 3, 45, 5)
  
       Team1 Win points
  1  Vikings  20     12
  2  Chicago  13      3
  3 GreenBay   9     45
  4  Detroit  12      5
  </code></pre>
  
  <p>to generate grouped bar plot, first we need to melt it:</p>
  
  <pre><code>library(reshape2)
  &gt; d.nfl.melt &lt;- melt(d.nfl[,c('Team1','Win','points')],id.vars = 1)
  &gt; ggplot(d.nfl.melt,aes(x = Team1,y = value)) + geom_bar(aes(fill = variable),position = "dodge", stat="identity") + coord_flip()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/uqou6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uqou6.png" alt="enter image description here"></a></p>
  
  <p>above ggplot is unordered.</p>
  
  <p>but how I do ordered group bar plot (ascending manner)</p>