title: Order of legend entries in ggplot2 barplots with coord_flip()
body: <p>I'm struggling get the right ordering of variables in a graph I made with ggplot2 in R.</p>
  
  <p>Suppose I have a dataframe such as:</p>
  
  <pre><code>set.seed(1234)
  my_df&lt;- data.frame(matrix(0,8,4))
  names(my_df) &lt;- c("year", "variable", "value", "vartype")
  my_df$year &lt;- rep(2006:2007)
  my_df$variable &lt;- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2))
  my_df$value &lt;- runif(8, 5,10) 
  my_df$vartype&lt;- c(rep("TA",4), rep("TB",4))
  </code></pre>
  
  <p>which yields the following table:</p>
  
  <pre><code>  year variable    value vartype
  1 2006       VX 5.568517      TA
  2 2007       VX 8.111497      TA
  3 2006       VB 8.046374      TA
  4 2007       VB 8.116897      TA
  5 2006       VZ 9.304577      TB
  6 2007       VZ 8.201553      TB
  7 2006       VD 5.047479      TB
  8 2007       VD 6.162753      TB
  </code></pre>
  
  <p>There are four variables (VX, VB, VZ and VD), belonging to two groups of variable types, (TA and TB).</p>
  
  <p>I would like to plot the values as horizontal bars on the y axis, <strong>ordered vertically first by variable groups and then by variable names</strong>, faceted by year, with values on the x axis and fill colour corresponding to variable group.
  (i.e. in this simplified example, the order should be, top to bottom, VB, VX, VD, VZ)</p>
  
  <p>1) My first attempt has been to try the following: </p>
  
  <pre><code>ggplot(my_df,        
      aes(x=variable, y=value, fill=vartype, order=vartype)) +
         # adding or removing the aesthetic "order=vartype" doesn't change anything
       geom_bar() + 
       facet_grid(. ~ year) + 
       coord_flip()
  </code></pre>
  
  <p>However, the variables are listed in reverse alphabetical order, but not by <strong>vartype</strong> : the <code>order=vartype</code> aesthetic is ignored.  </p>
  
  <p><img src="https://i.stack.imgur.com/GGZNG.png" alt="enter image description here"></p>
  
  <p>2) Following an answer to a similar question I posted yesterday, i tried the following, based on the post <a href="https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph">Order Bars in ggplot2 bar graph</a> :</p>
  
  <pre><code>my_df$variable &lt;- factor(
    my_df$variable, 
    levels=rev(sort(unique(my_df$variable))), 
    ordered=TRUE
  )
  </code></pre>
  
  <p>This approach does gets the variables in vertical alphabetical order in the plot, but ignores the fact that the variables should be ordered <strong>first by variable goups</strong> (with TA-variables on top and TB-variables below).</p>
  
  <p><img src="https://i.stack.imgur.com/z7hYe.png" alt="enter image description here"></p>
  
  <p>3) The following gives the same as 2 (above):</p>
  
  <pre><code>my_df$vartype &lt;- factor(
    my_df$vartype, 
    levels=sort(unique(my_df$vartype)), 
    ordered=TRUE
  )
  </code></pre>
  
  <p>... which has the same issues as the first approach (variables listed in reverse alphabetical order, groups ignored)</p>
  
  <p>4) another approach, based on  the original answer to <a href="https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph">Order Bars in ggplot2 bar graph</a> , also gives the same plat as 2, above</p>
  
  <pre><code>my_df &lt;- within(my_df, 
                  vartype &lt;- factor(vartype, 
                  levels=names(sort(table(vartype),
                  decreasing=TRUE)))
                  ) 
  </code></pre>
  
  <p>I'm puzzled by the fact that, despite several approaches, the aesthetic <code>order=vartype</code> is ignored. Still, it seems to work in an unrelated problem: <a href="http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/" rel="noreferrer">http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/</a></p>
  
  <p>I hope that the problem is clear and welcome any suggestions.</p>
  
  <p>Matteo</p>
  
  <p><em>I posted a similar question yesterday, but, unfortunately I made several mistakes when descrbing the problem and providing a reproducible example. 
  I've listened to several suggestions since, and <strong>thoroughly searched stakoverflow for similar question and applied, to the best of my knowledge, every suggested combination of solutions, to no avail.</strong> 
  I'm posting the question again hoping to be able to solve my issue and, hopefully, be helpful to others.</em></p>