title: How to assign colors using a variable containing color names in a grouped ggplot?
body: <p>In this simple example, I create a variable with the names of colors.</p>
  
  <pre><code>df &lt;- mtcars %&gt;%
    mutate(color = "green",
       color = replace(color, cyl==6, "blue"),
       color = replace(color, cyl==8, "red"))
  </code></pre>
  
  <p>Running the code below works as expected.</p>
  
  <pre><code>ggplot(df, aes(wt, mpg)) +
    geom_point(color = df$color)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tYdTk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tYdTk.png" alt="enter image description here"></a></p>
  
  <p>What if I want to use geom_line to create three lines--green, blue, and red?</p>
  
  <pre><code>ggplot(df, aes(wt, mpg, group=cyl)) +
    geom_line(color = df$color)
  </code></pre>
  
  <p>Instead, I get three lines with the colors cycling throughout.
  <a href="https://i.stack.imgur.com/XlMwq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XlMwq.png" alt="enter image description here"></a></p>
  
  <p>How can I use a variable with color names to assign the color of different lines?</p>