How to extract the fill colours from a ggplot object?

RGgplot2

R Problem Overview


I am trying to write some automated unit tests for a series of functions that generates ggplot graphics.

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.

The background:

Here is some example code, that set the fill colour to use the ColourBrewer palette Dark2:

p <- 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)

enter image description here

OK, so a visual inspection tells me the code worked.

What I've tried:

Now I want to confirm this by inspecting the object:

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   :<environment: R_GlobalEnv> 
 - attr(*, "class")= chr "ggplot"

Fine, the $scales object seems interesting. Let's look at that in more detail:

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

But here I draw a blank. There is nothing inside p$scales that looks like either my input palette, or in fact like colours.

What I expect to find:

The colours that I would expect are:

library(RColorBrewer)
brewer.pal(3, name="Dark2")
[1] "#1B9E77" "#D95F02" "#7570B3"

The question:

How do I interrogate a ggplot object for specific fill colours to use?

R Solutions


Solution 1 - R

Try building the plot,

g <- ggplot_build(p)
unique(g$data[[1]]["fill"])

      fill
1  #1B9E77
16 #D95F02
28 #7570B3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrieView Question on Stackoverflow
Solution 1 - RbaptisteView Answer on Stackoverflow