Jak wyodrębnić kolory wypełnienia z obiektu ggplot?
Próbuję napisać kilka automatycznych testów jednostkowych dla serii funkcji generujących ggplot
grafikę.
Na przykład chcę ustawić konkretną skalę kolorów na wykresie. Teraz potrzebuję sposobu, aby określić, czy rzeczywiście zastosowano właściwą skalę kolorów.
Tło:
Oto przykładowy kod, który ustawia fill
kolor na paletę 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)
OK, więc oględziny mówią ja kod zadziałał.
Co próbowałem:
Teraz chcę to potwierdzić, sprawdzając obiekt:
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"
Dobrze, obiekt wydaje się interesujący. Spójrzmy na to bardziej szczegółowo:
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
Ale tutaj rysuję pustkę. Nie ma nic w środku p$scales
, co wyglądałoby albo jak mój wkład palette
, albo w rzeczywistości jak kolory.
Czego oczekuję:
Kolory, których bym się spodziewał to:
library(RColorBrewer)
brewer.pal(3, name="Dark2")
[1] "#1B9E77" "#D95F02" "#7570B3"
Pytanie:
Jak Sprawdzam obiekt ggplot
pod kątem konkretnych kolorów wypełnienia?
54
1 answers
Spróbuj zbudować działkę,
g <- ggplot_build(p)
unique(g$data[[1]]["fill"])
fill
1 #1B9E77
16 #D95F02
28 #7570B3
72
Author: baptiste,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-02 09:47:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-02 09:47:00