If I understand your question correctly, you want to create a custom scale_...
function. In short, this is dipping somewhat deeper into the source code.
I will show one approach which will simply modify the existing functions in the packages RColorBrewer, scales, and of course ggplot2.
- The core is to modify
RcolorBrewer::brewer.pal
, which I guess is bascially what you want. Seeing the code, it selects from a list of colors the right one, depending on your "n". And this is what you need to manually create. I just copied the colors from the palette "YlOrBr". Something to consider: Brewer palettes are not random, they have been tested and created for being recognizable for printing, copying and colorblindness, so I am not sure if it is very clever to create your own palette. Have a look at https://colorbrewer2.org/ to find suitable palettes.
- modify the underlying color selector
scales:::brewer_pal
- modify the
scale_fill/scale_color
function
I've boiled down the functions to the cores, so they won't make the usual checks and are less flexible. You can modify the original functions to get this functionality back.
library(ggplot2)
mybrewerpal <- function(n, name) {# modified RcolorBrewer::brewer.pal
## the first call to switch would not be necessary in this example,
## but I leave it in order to make the underlying structure in brewer.pal clearer
switch(name, mypal = switch(n - 2, rgb(c(255, 254, 217), c(247, 196, 95), c(188, 79, 14), maxColorValue = 255),
rgb(c(255, 254, 254, 204), c(255, 217, 153, 76), c(212, 142, 41, 2), maxColorValue = 255),
rgb(c(255, 254, 254, 217, 153), c(255, 217, 153, 95, 52), c(212, 142, 41, 14, 4), maxColorValue = 255),
rgb(c(255, 254, 254, 254, 217, 153), c(255, 227, 196, 153, 95, 52), c(212, 145, 79, 41, 14, 4), maxColorValue = 255),
rgb(c(255, 254, 254, 254, 236, 204, 140), c(255, 227, 196, 153, 112, 76, 45), c(212, 145, 79, 41, 20, 2, 4), maxColorValue = 255),
rgb(c(255, 255, 254, 254, 254, 236, 204, 140), c(255, 247, 227, 196, 153, 112, 76, 45), c(229, 188, 145, 79, 41, 20, 2, 4), maxColorValue = 255),
rgb(c(255, 255, 254, 254, 254, 236, 204, 153, 102), c(255, 247, 227, 196, 153, 112, 76, 52, 37), c(229, 188, 145, 79, 41, 20, 2, 4, 6), maxColorValue = 255)
))
}
brewer_pal2 <- # modified from scales:::brewer_pal
function() { # stripped down all arguments, just to show the core
function(n) {
mybrewerpal(n, "mypal") ##modified, usually this is selected by a function
## with type and name as arguments, selecting a palette from a list called scales:::brewer
}
}
scale_fill_custom <- ### modified from scale_fill_brewer, removed some arguments
function (..., aesthetics = "fill") {
discrete_scale(aesthetics, "custom", brewer_pal2(), ...) ## give a new name to the
## scale, it will create a new Scale object.
}
p <-
ggplot(mtcars, aes(x = mpg, y = disp)) +
scale_fill_custom()
p + geom_point(shape = 21, aes(fill = as.factor(cyl)))
p + geom_point(shape = 21, aes(fill = as.factor(carb)))
scale_fill_manual(..., values=colors)
– Antoniaantonie