Reputation: 1599
When I use a diverging color scale gradient with scale_color_distiller()
I want to set the midpoint color value on the average of each value on the x-axis.
Here you see a part of the original chart to get the idea why I want to do this. The black stripes are averages. I want the midpont of the diverging scale on that precise value.
Here's some example data and code:
library(tidyverse)
example_df <- tibble::tribble(
~id, ~level, ~weight,
1, "20", 90,
2, "20", 80,
3, "20", 50,
4, "40", 75,
5, "40", 50,
6, "40", 35
)
example_df %>%
ggplot(aes(level, weight,
color = weight)) +
geom_point(size = 20,
shape = 95) +
scale_color_distiller(palette = "Spectral",
direction = 1) +
theme_minimal()
Resulting in:
Thanks in advance.
Upvotes: 1
Views: 350
Reputation: 23767
You basically want to use color scale for your percentiles and not for absolute values. So convert your weight
values into percentiles first.
If you really need to use scale_color_distiller
rather than scale_color_gradientn
, and want the midpoint at your median, than you should expand your data frame in order to have values for each percentile.
If you are not bound to use the distiller scale, you could also set your midpoint to 0.5 in scale_color_gradientn
.
library(tidyverse)
df1 <-
example_df %>%
group_by(level) %>%
mutate(perc = ntile(weight,10))
# works already ok, but this doesn't give the exact midpoint.
ggplot(df1, aes(level, weight, color = perc)) +
geom_point(size = 20, shape = 95) +
scale_color_distiller(palette = "Spectral", direction = 1)
# expanding the data frame to include all deciles
df2 <- df1 %>% complete(perc = 0:10)
ggplot(df2, aes(level, weight, color = perc)) +
geom_point(size = 20, shape = 95) +
scale_color_distiller(palette = "Spectral", direction = 1)
#> Warning: Removed 16 rows containing missing values (geom_point).
Created on 2020-01-02 by the reprex package (v0.3.0)
Upvotes: 1