Closed andrewheiss closed 2 years ago
Yeah... I've been thinking about this as well, so thanks for reminding me :-) we definitely need this
+1 would be great!
+1 would be great!
+1 would be great, any progress here?
I whipped something up myself for my own purposes (slightly different than what OP is asking for---I had continuous data). If you (@thomasp85 ) want me to make a pull request I can.
Set mid
to the value you want as the midpoint. This works for continuous data only, though!
scale_fill_scico_mid <- function(..., mid = 0, alpha = NULL, begin = 0, end = 1, direction = 1, palette = "broc") {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 is required for this functionality", call. = FALSE)
}
force(mid)
ggplot2::continuous_scale(
aesthetics = "fill",
scale_name = "gradient2",
palette = scales::gradient_n_pal(
colours = scico(256, alpha, begin, end, direction, palette),
values = NULL, space = "Lab"),
guide="colourbar",
rescaler = function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
scales::rescale_mid(x, to, from, mid)
},
...
)
}
fixed by 3777e5f1982bdc91
There are several diverging color palettes like broc, cork, vic, etc. where the center color value is as meaningful as the extremes.
However, if you use
scale_*_scico()
on a variable that's not symmetrically distributed around zero, the colors also won't be centered around zero:One workaround is to extract the extreme and center values from the palette with
scico(3)
and usescale_fill_gradientn()
to interpolate colors between low, mid, and high. This unfortunately loses the magical range of colors in the palette that makes scico so greatThe better solution is to use the
limits
argument to make the scale be symmetrical, but doing so requires additional code and math to workWould it be worth it to add an argument like
center = TRUE
orcenter = 0
toscale_*_scico()
for the diverging palettes, or should users handle the centering on their own?I haven't found any other examples of how this is done in other packages. None of the viridis palettes are diverging, and the ColorBrewer diverging palettes that are accessible with
scale_*_brewer(type = "div", palette = "PuOr")
only work with discrete scales (which lets ggplot get around this centering issue)