title: Assign stable colors to ggplot when reordering and subsetting
body: <p>In a shiny app, I let users subset data, and then create a plot in which a categorical variable is reordered from higher to lower according to the value of another variable and displayed following the user input. </p>
  
  <p>I would like color and fill to be linked to the category variable <em>irrespective of the order</em>.</p>
  
  <p>Tried and not working solutions:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/6919025/how-to-assign-colors-to-categorical-variables-in-ggplot2-that-have-stable-mappin#comment8263449_6924503">How to assign colors to categorical variables in ggplot2 that have stable mapping?</a> </p></li>
  <li><p><a href="https://stackoverflow.com/questions/31515024/how-to-manually-set-colours-to-a-categorical-variables-using-ggplot">How to manually set colours to a categorical variables using ggplot()?</a> </p></li>
  </ul>
  
  <p>MWE:</p>
  
  <pre><code>
  data &lt;- mtcars %&gt;% 
    rownames_to_column() %&gt;% 
    rowid_to_column() %&gt;% 
    mutate(rowname = reorder(rowname, mpg))
  
  plot &lt;- data %&gt;% 
    ggplot(aes(rowname, mpg, fill = rowname, color = rowname))+
    geom_col()+
    coord_flip()
  
  plot %+% droplevels(filter(data, rowid &lt; 3))
  </code></pre>
  
  <p>produces this:</p>
  
  <p><a href="https://i.stack.imgur.com/zxHsn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zxHsn.png" alt="enter image description here"></a></p>
  
  <p>while </p>
  
  <pre><code>plot %+% droplevels(filter(data, rowid &lt; 4))
  </code></pre>
  
  <p>produces this: </p>
  
  <p><a href="https://i.stack.imgur.com/J7oYF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J7oYF.png" alt="enter image description here"></a></p>
  
  <p>In a nutshell: </p>
  
  <p><strong>Intended behavior</strong>: colors are matched to category irrespective of order of the plot. </p>
  
  <p><strong>Actual behavior</strong>: colors change depending on position of the category once <code>reorder</code>ed.</p>