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.
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"
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.
The colours that I would expect are:
library(RColorBrewer) brewer.pal(3, name="Dark2") [1] "#1B9E77" "#D95F02" "#7570B3"
How do I interrogate a ggplot
object for specific fill colours to use?
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).
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.
By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.
By default, ggplot graphs use a black color for lines and points and a gray color for shapes like the rectangles in bar graphs.
Try building the plot,
g <- ggplot_build(p) unique(g$data[[1]]["fill"]) fill 1 #1B9E77 16 #D95F02 28 #7570B3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With