title: How to order facets based on facet_wrap from Y values?
body: <p>is there a way to reorder those facets based on their Y value (aka <code>count</code> ) in decreasing order dynamically - without hardcoding the order (given a similar column to <code>package</code> with hundreds of levels?</p>
  <pre><code>  library(cranlogs)
    library(tidyverse)   
  
    tidyverse_downloads &lt;- cran_downloads(
        packages = pkgs,
        from     = &quot;2017-01-01&quot;,
        to       = &quot;2017-06-30&quot;) %&gt;%
        tibble::as_tibble() %&gt;%
        group_by(package)
  
  
  
    tidyverse_downloads %&gt;%
       ggplot(aes(x = date, y = count)) +
       #geom_hline(yintercept = 0, color = &quot;grey40&quot;) +
       geom_line() +
       #geom_smooth(method = &quot;loess&quot;) +
       theme_minimal() +
       facet_wrap(~ package, scale = &quot;free_y&quot;,ncol = 2) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
  </code></pre>
  <p><a href="https://i.stack.imgur.com/CdvOy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CdvOy.png" alt="
  " /></a></p>
  <p>What I have tried was to use the package library(tidytext) and <code>scale_y_reordered</code></p>
  <pre><code>   tidyverse_downloads %&gt;%
       mutate(count = tidytext::reorder_within(count, date, package)) %&gt;% 
       tidytext::scale_y_reordered() +
       ggplot(aes(x = date, y = count)) +
       geom_line() +
       theme_minimal() +
       facet_wrap(~ package, scale = &quot;free_y&quot;,ncol = 2) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
  </code></pre>
  <p>however, that doesn't produce any outcome. Im doing something wrong I guess. Appreciate any help</p>