title: how to reorder a factor in a dataframe with fct_reorder?
body: <p>Consider the following example</p>
  
  <pre><code>&gt; library(forcats)
  &gt; library(dplyr)
  &gt; 
  &gt; 
  &gt; dataframe &lt;- data_frame(var = c(1,1,1,2,3,4),
  +                         var2 = c(10,9,8,7,6,5))
  &gt; dataframe
  # A tibble: 6 x 2
      var  var2
    &lt;dbl&gt; &lt;dbl&gt;
  1  1.00 10.0 
  2  1.00  9.00
  3  1.00  8.00
  4  2.00  7.00
  5  3.00  6.00
  6  4.00  5.00
  </code></pre>
  
  <p>I create a factor variable</p>
  
  <pre><code>&gt; dataframe &lt;- dataframe %&gt;% mutate(myfactor = factor(var))
  &gt; 
  &gt; dataframe$myfactor
  [1] 1 1 1 2 3 4
  Levels: 1 2 3 4
  </code></pre>
  
  <p>I do not understand what is the correct syntax (and the logic) to reorder this factor according to some other computation done at the factor level.</p>
  
  <p>For instance, I would like to reorder my factors according to decreasing values of </p>
  
  <pre><code>&gt; data_rank &lt;- dataframe %&gt;% group_by(myfactor) %&gt;% summarise(rank_var = mean(var2))
  
  &gt; data_rank
  # A tibble: 4 x 2
    myfactor rank_var
    &lt;fct&gt;       &lt;dbl&gt;
  1 1            9.00
  2 2            7.00
  3 3            6.00
  4 4            5.00
  </code></pre>
  
  <p>So 4 would be first, 3 would be second, etc.</p>
  
  <p>What is the syntax to do so with <code>fct_reorder</code>, and what is the logic behind it?</p>
  
  <p>Thanks!</p>