title: How to extract the fill colours from a ggplot object?
body: <p>I am trying to write some automated unit tests for a series of functions that generates <code>ggplot</code> graphics.</p>
  
  <p>For example, I want to set a specific colour scale to a plot. Now I need a way to determine whether the correct colour scale was actually applied.</p>
  
  <h2>The background:</h2>
  
  <p>Here is some example code, that set the <code>fill</code> colour to use the ColourBrewer palette <code>Dark2</code>:</p>
  
  <pre><code>p &lt;- ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(gear))) + 
    geom_bar(stat="identity") + 
    facet_grid(~gear) +
    scale_fill_brewer(palette="Dark2")
  
  print(p)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/GHqdo.png" alt="enter image description here"></p>
  
  <p>OK, so a visual inspection tells me the code worked.</p>
  
  <h2>What I've tried:</h2>
  
  <p>Now I want to confirm this by inspecting the object:</p>
  
  <pre><code>str(p, max.level=1)
  List of 8
   $ data       :'data.frame':    32 obs. of  11 variables:
   $ layers     :List of 1
   $ scales     :Reference class 'Scales' [package "ggplot2"] with 1 fields
    ..and 20 methods, of which 9 are possibly relevant
   $ mapping    :List of 3
   $ options    :List of 1
   $ coordinates:List of 1
    ..- attr(*, "class")= chr [1:2] "cartesian" "coord"
   $ facet      :List of 9
    ..- attr(*, "class")= chr [1:2] "grid" "facet"
   $ plot_env   :&lt;environment: R_GlobalEnv&gt; 
   - attr(*, "class")= chr "ggplot"
  </code></pre>
  
  <p>Fine, the <code>$scales</code> object seems interesting. Let's look at that in more detail:</p>
  
  <pre><code>str(p$scales)
  Reference class 'Scales' [package "ggplot2"] with 1 fields
   $ scales:List of 1
    ..$ :List of 14
    .. ..$ call      : language discrete_scale(aesthetics = "fill", scale_name = "brewer", palette = brewer_pal(type, palette))
    .. ..$ aesthetics: chr "fill"
    .. ..$ scale_name: chr "brewer"
    .. ..$ palette   :function (n)  
    .. ..$ range     :Reference class 'DiscreteRange' [package "scales"] with 1 fields
    .. .. ..$ range: NULL
    .. .. ..and 14 methods, of which 3 are possibly relevant:
    .. .. ..  initialize, reset, train
    .. ..$ limits    : NULL
    .. ..$ na.value  : logi NA
    .. ..$ expand    : list()
    .. .. ..- attr(*, "class")= chr "waiver"
    .. ..$ name      : NULL
    .. ..$ breaks    : list()
    .. .. ..- attr(*, "class")= chr "waiver"
    .. ..$ labels    : list()
    .. .. ..- attr(*, "class")= chr "waiver"
    .. ..$ legend    : NULL
    .. ..$ drop      : logi TRUE
    .. ..$ guide     : chr "legend"
    .. ..- attr(*, "class")= chr [1:3] "brewer" "discrete" "scale"
   and 20 methods, of which 9 are possibly relevant:
     add, clone, find, get_scales, has_scale, initialize, input, n, non_position_scales
  </code></pre>
  
  <p>But here I draw a blank. There is nothing inside <code>p$scales</code> that looks like either my input <code>palette</code>, or in fact like colours.</p>
  
  <h2>What I expect to find:</h2>
  
  <p>The colours that I would expect are:</p>
  
  <pre><code>library(RColorBrewer)
  brewer.pal(3, name="Dark2")
  [1] "#1B9E77" "#D95F02" "#7570B3"
  </code></pre>
  
  <h2>The question:</h2>
  
  <p><strong>How do I interrogate a <code>ggplot</code> object for specific fill colours to use?</strong></p>