title: Change colours to defined palette for ggplot objects
body: <p>I would like to change the default colours to a specific palette for all <code>geom_*</code> type objects by using a function.</p>
  
  <p>Below is an example for <code>geom_line()</code> and using the function <code>change_colours()</code></p>
  
  <pre><code># load ggplot2 and tidyr library
  require(ggplot2)
  require(tidyr)
  
  # create a mock data frame
  df &lt;- data.frame(cbind(var1=500*cumprod(1+rnorm(300, 0, 0.04)),
                         var2=400*cumprod(1+rnorm(300, 0, 0.04)),
                         var3=300*cumprod(1+rnorm(300, 0, 0.04))))
  df$TS &lt;- as.POSIXct(Sys.time()+seq(300))
  df &lt;- gather(df, stock, price, -TS)
  
  # create basic base graph
  p &lt;- ggplot(df, aes(x=TS, y=price, group=stock))+geom_line(aes(colour=stock))
  
  # custom pallet
  custom_pal &lt;- c("#002776", "#81BC00", "#00A1DE", "#72C7E7", "#3C8A2E", "#BDD203", 
                  "#313131", "#335291", "#9AC933", "#33B4E5", "#8ED2EC", "#63A158", 
                  "#CADB35", "#575757", "#4C689F", "#A7D04C", "#4CBDE8", "#9DD8EE", 
                  "#76AD6D", "#D1DF4F", "#8C8C8C", "#7F93BA", "#C0DE80", "#80D0EE", 
                  "#B8E3F3", "#9DC496", "#DEE881", "#B4B4B4", "#99A9C8", "#CDE499", 
                  "#99D9F2", "#C7E9F5", "#B1D0AB", "#E5ED9A", "#DCDCDC")
  
  # the function the change colours
  change_colours &lt;- function(ggplot_obj, pal){
    p &lt;- ggplot_obj
    group_data &lt;- p$data[, as.character(p$mapping$group)]
    n_groups &lt;- length(unique(group_data))
    group_data_cols &lt;- pal[group_data]
  
    p + theme_light()+ geom_line(colour=group_data_cols)
  }
  
  p
  p1 &lt;- change_colours(ggplot_obj=p, pal=custom_pal)
  p1
  </code></pre>
  
  <p>I am hoping to change the <code>change_colours()</code> function to be more generic for all <code>geom_*</code> type layers</p>
  
  <p>Any help would be much appreciated.</p>