如何从ggplot对象中提取填充颜色? [英] How to extract the fill colours from a ggplot object?
问题描述
我正在尝试为一系列生成 ggplot
图形的函数编写一些自动单元测试。
例如,我想将一个特定的色阶设置为一个绘图。现在,我需要一种方法来确定是否实际应用了正确的色阶。
背景:
是一些示例代码,它将 fill
颜色设置为使用ColourBrewer调色板 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)
好的,所以视觉检查告诉我代码已经工作。
我试过了:
现在我想通过检查对象来确认这一点:
str(p,max.level = 1)
8
$ data的列表:'data.frame':32 obs。 11个变量:
$ layers:1
$的列表scale:引用类'Scales'[packageggplot2]带1个字段
..和20个方法,其中9个可能相关
$映射:3
$选项列表:1
$坐标列表:1
..- attr(*,class)= chr [1 :2]cartesiancoord
$ facet:9
..- attr(*,class)= chr [1:2]gridfacet
$ plot_env:< environment:R_GlobalEnv>
- attr(*,class)= chrggplot
$ scales
对象似乎很有趣。让我们更详细地看看:
str(p $ scales)
引用类'Scales'[package' ggplot2]带有1个字段
$ scales:1
.. $的列表$:$ 14的列表
.. .. $ call:language discrete_scale(aesthetics =fill,scale_name = brewer,palette = brewer_pal(type,palette))
.. .. $ aesthetics:chrfill
.. .. $ scale_name:chrbrewer
.. .. $ palette:function(n)
.. .. $ range:引用类'DiscreteRange'[packagescales]带有1个字段
.. .. .. $ range:NULL
.. ..和14个方法,其中3个可能是相关的:
.. .. ..初始化,重置,训练
.. .. $ limits:NULL
.. .. $ na.value:logi NA
.. .. $ expand:list()
.. .. attr(*,class)= chr豁免
$ .. $ name:NULL
.. .. $ breaks:list()
.. ..- attr(*,class)= chr豁免
.. .. $ labels:list()
.. ..- attr(*,class)= chr 豁免
.. .. $ legend:NULL
.. .. $ drop:logi TRUE
.. .. $ guide:chrlegend
..。 .- attr(*,class)= chr [1:3]brewerdiscretescale
和20个方法,其中9个可能相关:
add,clone,find ,get_scales,has_scale,initialize,input,n,non_position_scales
但是这里画了一个空白。 p $ scales
中没有任何东西看起来像我的输入 palette
,或者实际上就像颜色一样。
我期望找到的是什么:
我期望的颜色是:
library(RColorBrewer)
brewer.pal(3,name =Dark2)
[1]#1B9E77#D95F02 #7570B3
问题:
如何查询 ggplot
对象以使用特定的填充颜色?
尝试构建情节,
g < - ggplot_build(p)
独特(g $ data [[1]] [fill])
fill
1#1B9E77
16#D95F02
28#7570B3
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)
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?
Try building the plot,
g <- ggplot_build(p)
unique(g$data[[1]]["fill"])
fill
1 #1B9E77
16 #D95F02
28 #7570B3
这篇关于如何从ggplot对象中提取填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!