I'm trying to generate different palettes for different number of levels, based on the documentation as best as I understand it.
data(iris)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point() +
theme(palette.colour.discrete = list(c("#d12e82ff", "#66A61E"),
c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854")))
# last line is the result of RColorBrewer::brewer.pal(5, "Set2"), it produces the same error
This error results:
Error in `plot_theme()`:
! The `palette.colour.discrete` theme element must be a <character/function>
object.
This works fine, producing a plot with the specified palette:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point() +
theme(palette.colour.discrete = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854")))
As the error suggests, your used structure is not a character vector nor a function, in other words you can't use list in that context, at least not directly.
Though a function (as in function object that ggplot can call with a required number of levels) is a perfectly valid argument, so you could create a wrapper function instead:
# function factory to return a function
my_pal_fun <- function(pal_lst){
# pal_lst is stored in the enclosed environment of returned function
function(n){
idx <- which(n == lengths(pal_lst))
if(length(idx)){
pal_lst[[idx]]
} else {
# if list does not include n-colour palette,
# generate it with brewer.pal()
RColorBrewer::brewer.pal(n, "Set2")
}
}
}
my_palettes <- list(
c("#d12e82ff", "#66A61E"),
c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854")
)
# does not return a palette but a function:
my_pal_fun(my_palettes)
#> function (n)
#> {
#> idx <- which(n == lengths(pal_lst))
#> if (length(idx)) {
#> pal_lst[[idx]]
#> }
#> else {
#> RColorBrewer::brewer.pal(n, "Set2")
#> }
#> }
#> <environment: 0x0000020d29da5188>
# calling function from `my_pal_fun(my_palettes)` with n arg:
my_pal_fun(my_palettes)(2) # from list
#> [1] "#d12e82ff" "#66A61E"
my_pal_fun(my_palettes)(5) # from list
#> [1] "#66C2A5" "#FC8D62" "#8DA0CB" "#E78AC3" "#A6D854"
# 3-colour palette does exist in a list,
# generate it with RColorBrewer::brewer.pal(3, "Set2")
my_pal_fun(my_palettes)(3)
#> [1] "#66C2A5" "#FC8D62" "#8DA0CB"
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point() +
theme(palette.colour.discrete = my_pal_fun(my_palettes))
If you are just after a Colour Brewer palette, perhaps you don't need to fiddle with your own hardcoded lists and can use scales::pal_brewer() instead:
Indeed, you are correct about what the error message is telling me. As shown by my last example, I am aware a single character vector will work. However, the documentation indicates that a list can be used:
type The preferred mechanism for setting the default palette is by using the theme. For example: theme(palette.colour.discrete = "Okabe-Ito"). One of the following:
A character vector of color codes. The codes are used for a 'manual' color scale as long as the number of codes exceeds the number of data levels (if there are more levels than codes, scale_colour_hue()/scale_fill_hue() are used to construct the default scale). If this is a named vector, then the color values will be matched to levels based on the names of the vectors. Data values that don't match will be set as na.value.
A list of character vectors of color codes. The minimum length vector that exceeds the number of data levels is chosen for the color scaling. This is useful if you want to change the color palette based on the number of levels.
A function that returns a discrete colour/fill scale (e.g., scale_fill_hue(), scale_fill_brewer(), etc).
The documentation does not indicate indicated exactly how conditions are changed using the superseded function.
This list format did work fine in the (currently deprecated) options syntax, but then it sets ggplotting conditions globally instead of for a single plot:
opts <- options(
ggplot2.discrete.colour = list(
c("#d12e82ff", "#66A61E"), # for 2 levels
RColorBrewer::brewer.pal(5, "Set2"), # for 3-5 levels
RColorBrewer::brewer.pal(8, "Paired"), # for 6-8 levels
)
)
I appreciate the effort to show me how Rcolorbrewer palettes work, but as indicted in my original post, I understand that, and this is not the problem.
type?
I don't think you can expand type's description to theme's palette. I'm interpreting that part of documentation as "prefer theme(palette.colour.discrete = col_vec_or_function) over scale_colour_discrete(type = col_vec_or_list_or_function) "
(type() is an argument for the deprecated function.)
It doesn't look like this option is possible under the superseded function. It would help if the ggplot2 documentation was updated to reflect this (I've filed an issue). I assume it is not easy to adapt a list, or they would have done it. I'll close this issue and consider other options.