title: Associate a color palette with ggplot2 theme
body: <p>I want my ggplot2 theme to use a specific set of colors, but don't see how to avoid a separate line outside of the theme.</p>
  
  <p>I have this data:</p>
  
  <pre><code>library(ggplot2)
  mycars &lt;- mtcars
  mycars$cyl &lt;- as.factor(mycars$cyl)
  </code></pre>
  
  <p>And here's a dummy theme I plot with:</p>
  
  <pre><code>mytheme &lt;- theme(panel.grid.major = element_line(size = 2))
  
  ggplot(mycars, aes(x = wt, y = mpg)) +
    geom_point(aes(color = cyl)) +
    mytheme
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/sQaY2m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/sQaY2m.png" alt="without custom colors"></a></p>
  
  <p>I want the point colors to default to my custom palette:</p>
  
  <pre><code>mycolors &lt;- c("deeppink", "chartreuse", "midnightblue")
  </code></pre>
  
  <p>Can I somehow add that to my ggplot2 theme so that I don't constantly repeat this extra line of code at the end:</p>
  
  <pre><code>ggplot(mycars, aes(x = wt, y = mpg)) +
    geom_point(aes(color = cyl)) +
    mytheme +
    scale_color_manual(values = mycolors)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wy6SCm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wy6SCm.png" alt="with colors"></a></p>
  
  <p>I tried:</p>
  
  <pre><code>mytheme2 &lt;- mytheme + scale_color_manual(values = mycolors)
  </code></pre>
  
  <p>But got:</p>
  
  <blockquote>
    <p>Error: Don't know how to add scale_color_manual(values = mycolors) to
    a theme object</p>
  </blockquote>