Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

r - How to extract the fill colours from a ggplot object?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try building the plot,

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...