title: Sort ggplot facet_wrap by color?
body: <p>I would like to sort by <code>ggplot</code> <code>facet_wrap</code> by color.</p>
  
  <p>For example, in this demo code, the color corresponds to groups A, B, C. I am looking to have all the red plots next to each other, and same for the blue and green plots.</p>
  
  <p>I tried sorting my data by group but <code>ggplot</code> seems to switch the order when plotting.</p>
  
  <pre><code>library(tidyverse)
  set.seed(42)
  
  # Generate example data frame
  id &lt;- 1:15
  data &lt;- map(id, ~rnorm(10))
  date &lt;- map(id, ~1:10)
  group &lt;- map_chr(id, ~sample(c('a','b','c'), size=1))
  df &lt;- tibble(id=id, data=data, date=date, group=group) %&gt;% unnest(cols = c(data, date))
  
  # Generate plot
  df %&gt;%
    arrange(group) %&gt;%
    ggplot(mapping = aes(x=date, y=data, color=group)) +
    geom_line() +
    geom_point() +
    facet_wrap(~ id)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/70c7T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/70c7T.png" alt="enter image description here"></a></p>