Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the fill colours from a ggplot object?

Tags:

r

ggplot2

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?

like image 222
Andrie Avatar asked Aug 02 '12 09:08

Andrie


People also ask

What does fill in Ggplot do?

Generally, fill defines the colour with which a geom is filled, whereas colour defines the colour with which a geom is outlined (the shape's "stroke", to use Photoshop language).

Is Ggplot colorblind friendly?

The default colors in ggplot2 can be difficult to distinguish from one another because they have equal luminance. They are also not friendly for colorblind viewers.

What are Ggplots default colors?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.

What colors does Ggplot recognize?

By default, ggplot graphs use a black color for lines and points and a gray color for shapes like the rectangles in bar graphs.


1 Answers

Try building the plot,

g <- ggplot_build(p) unique(g$data[[1]]["fill"])        fill 1  #1B9E77 16 #D95F02 28 #7570B3 
like image 73
baptiste Avatar answered Sep 24 '22 14:09

baptiste