title: Change the order of Stacked Bar Chart in ggplot2
body: <p>I've been struggling for a few hours to change the order of stacked bar chart. </p>
  
  <p>I looked at two SO threads: <a href="https://stackoverflow.com/questions/33538059/how-to-change-stacking-order-in-stacked-bar-chart-in-r">How to change stacking order in stacked bar chart in R?</a> and <a href="https://stackoverflow.com/questions/32345923/how-to-control-ordering-of-stacked-bar-chart-using-identity-on-ggplot2">How to control ordering of stacked bar chart using identity on ggplot2</a></p>
  
  <p>Both of these threads use rev() function in factor(). However, for some reason, this doesn't work for me. </p>
  
  <p>Here's my code: 
  (The version in which "D" appears at the top in bar and in legend)</p>
  
  <pre><code>a&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(1,2,3,4)))
  b&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(4,3,2,1)))
  d&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(5,8,9,10)))
  
  a$type &lt;- c("A")
  b$type &lt;- c("B")
  d$type &lt;- c("d")
  c&lt;-rbind(a,b,d)
  c$type &lt;- factor(c$type,levels = rev(c$type))
  
  windows()
  ggplot(data = c, aes(x = ID, y = Num, fill = type)) + 
    geom_bar(stat = "identity")
  </code></pre>
  
  <p>Now, I want to reverse this by having "A" at the top both in the bar chart and in the legend. 
  I tried this code:</p>
  
  <pre><code>a&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(1,2,3,4)))
  b&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(4,3,2,1)))
  d&lt;-as.data.frame(list(ID=c("Q1","Q2","Q3","Q4"),Num = c(5,8,9,10)))
  
  a$type &lt;- c("A")
  b$type &lt;- c("B")
  d$type &lt;- c("d")
  c&lt;-rbind(a,b,d)
  c$type &lt;- factor(c$type,levels = c$type)
  
  
  
  windows()
  ggplot(data = c, aes(x = ID, y = Num, fill = type)) + 
    geom_bar(stat = "identity")
  </code></pre>
  
  <p>This reverses the order in the legend but not the order in the bar chart. I still see the order D-> B -> A. I am really frustrated. </p>
  
  <p>Can someone please help me? I'd appreciate your thoughts.</p>