title: Assigning specific colors to specific cases in ridgeline plots in R
body: <p>Recently this community helped me tremendously with getting Ridgeline plots to work with my data.
  Now I am struggling with coloring them according to my needs.
  Basically what I want is plotting my cases in different orders but they should keep a specific color so observations remain recognizable even when plotted in a different order. So far I failed with applying the available solutions to my requirements.</p>
  <p>Let us take for example this data, where we have a name, a mean and an SD:</p>
  <pre><code>caseName    caseMean    caseSD
  Svansdottir 2006    -0.0646 0.4032398
  Guétin 2009 -0.4649 0.3995663
  Raglio 2010a    -0.2145 0.2814031
  </code></pre>
  <p>Let's first sort them by caseMean:</p>
  <pre><code>df$caseName &lt;- factor(df$caseName, levels = df$caseName[order(df$caseMean)])
  </code></pre>
  <p>and plot it with the following code:</p>
  <pre><code>library(tidyverse); library(ggridges)
  n = 100
  df3 &lt;- df %&gt;%
    mutate(low  = caseMean - 3 * caseSD, high = caseMean + 3 * caseSD) %&gt;%
    uncount(n, .id = &quot;row&quot;) %&gt;%
    mutate(x    = (1 - row/n) * low + row/n * high, 
           norm = dnorm(x, caseMean, caseSD))
  ggplot(df3, aes(x, caseName, height = norm, fill=caseName)) +
    geom_ridgeline(scale = 2,alpha=0.75) +
    scale_fill_viridis_d()
  </code></pre>
  <p>we get this: <img src="https://i.stack.imgur.com/vhVC4.png" alt="sorted by caseMean" /></p>
  <p>Now we reverse the order</p>
  <pre><code>df$caseName &lt;- factor(df$caseName, levels = df$caseName[order(-df$caseMean)])
  </code></pre>
  <p>and plot again with the code above we see that the plots have switched color: <img src="https://i.stack.imgur.com/baZ9C.png" alt="cases in reverse order" /></p>
  <p>How can I make sure that the same cases have always the same colors no matter the order I put them in?</p>
  <p>I would like to have code that doesn't require me to to &quot;hard-wire&quot; colors to a specific case name. I want to be able to do this to ridgeline plots with 20, 30, or more observations. The fact that I picked the viridis color palette doesn't matter. I am happy with any solution (like with heat.colors or something similar).</p>