I want to create my own default color scheme/template for ggplot2
(think corporate colors) which is used automatically if the user loads the package.
The system works if all colors are used. If however only a subset of the colors are used, ggplot2, by default, selects the colors from the beginning. Is there a way to select the colors by another function? (E.g., when selecting two colors from the color list, select the first and the last. For three colors select the first, the middle, and the last, etc)
This should be done automatically, so that the user doesn't need to specify the colors herself.
To give an example of what I did so far:
library(ggplot2)
library(dplyr)
# in .onAttach as this is used within a package
my_colors <- c("red", "orange", "yellow", "green", "blue", "violet")
assign("scale_color_discrete", function(..., values = my_colors) scale_color_manual(..., values = as.character(values)), globalenv())
assign("scale_colour_discrete", function(..., values = my_colors) scale_colour_manual(..., values = as.character(values)), globalenv())
assign("scale_fill_discrete", function(..., values = my_colors) scale_fill_manual(..., values = as.character(values)), globalenv())
df <- mtcars %>%
mutate(carb = as.factor(carb))
df %>%
ggplot(aes(x = wt, y = mpg, color = carb)) +
geom_point()
Good figure: all 6 colors are used (~the whole spectrum of my colors are used).
df %>%
filter(carb %in% c(2, 3, 4)) %>%
ggplot(aes(x = wt, y = mpg, color = carb)) +
geom_point()
Bad figure: the first three colors are used. The goal here is to have colors 1, 3 (or 4), 6 selected by the scale_color_manual()
function to "select" the whole spectrum.
Created on 2020-01-17 by the reprex package (v0.3.0)
Any ideas how to select the right colors?