如何从 ggplot 对象中提取填充颜色? [英] How to extract the fill colours from a ggplot object?

查看:23
本文介绍了如何从 ggplot 对象中提取填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一系列生成 ggplot 图形的函数编写一些自动化单元测试.

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.

这是一些示例代码,设置fill颜色以使用ColourBrewer调色板Dark2:

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)

好的,目视检查告诉我代码有效.

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

现在我想通过检查对象来确认这一点:

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"

好吧,$scales 对象看起来很有趣.让我们更详细地了解一下:

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

但在这里我画了一个空白.p$scales 中没有任何东西看起来像我的输入 palette,或者实际上像颜色.

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

我期望的颜色是:

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

问题:

如何查询 ggplot 对象以获取要使用的特定填充颜色?

The question:

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

推荐答案

尝试构建情节,

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

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

这篇关于如何从 ggplot 对象中提取填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆