New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: midpoint argument in scale_color_gradientn() #3738
Comments
It's not clear to me that adding a In the same vein, maybe |
Just a thought on your last idea. I don’t think |
Lines 74 to 78 in 214f314
Lines 93 to 99 in 214f314
You just made my case for renaming |
Haha - I guess I have been starring at code for too long |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Here's how I approached this problem, following the example from this related Stack Overflow question: There's an internal function in ggplot2, library("ggplot2")
mid_rescaler <- function(mid = 0) {
function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
scales::rescale_mid(x, to, from, mid)
}
}
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180) - .7)
ggplot(grid, aes(lon, lat)) +
geom_raster(aes(fill = z)) +
scale_fill_distiller(palette = "RdBu", rescaler = mid_rescaler()) Created on 2020-02-08 by the reprex package (v0.3.0) As you know, the The Could it be useful to make such a function available, publicly, in ggplot2? Also thanks to @dpseidel, who listened very patiently as I waved my hands trying to describe this at rstudio::conf, while the right approach was to make a reprex. |
This appears to do the trick - I used a tip from tidyverse/ggplot2#3738 (comment) @gritzenthaler would you mind testing this out to make sure I didn't miss anything?
Hi, say I built a plot where the Y axis maps a numerical variable and the X axis a factor variable of 4 levels (i.e. 4 groups). Then I decide to map the data with a geom_jitter and colour the plot with a divergent gradient. My question is: is there a way for the midpoint of the divergent scale colour to start exactly at the median of each level (for this of course the legend would not make sense but as a way of visualising medians with colours). |
Because the library("ggplot2")
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180) - .7)
ggplot(grid, aes(lon, lat)) +
geom_raster(aes(fill = z)) +
scale_fill_distiller(
palette = "RdBu",
rescaler = ~ scales::rescale_mid(.x, mid = 0)
) Created on 2022-12-03 by the reprex package (v2.0.1) |
Since there are now divergent continuous color palettes available in different packages, it would be nice to be able to specify a midpoint without manually having to calculate the limits.
related issue: thomasp85/scico#6
The text was updated successfully, but these errors were encountered: