Often I would like to bin a continuous variable when I plot it with ggplot. In the example below, it will be the fill aesthetic. It seems that both scale_fill_binned and scale_fill_stepn should be able to do the job yet I struggle to manually specify breaks and colors for data that doesn't plot neatly on a linear scale.
In the example below, both very small negative numbers as well as large positive ones are present. So I might want to have a distinct color for negative values (say, red) and a higher resolution for small positive values than in the default of scale_fill_binned.
However, when I manually add breaks the lower three bins all have the same color. What would be the best way to bin continuous data while maintaining some control over the position of the breaks and the color scheme? Is it necessary to go fully manual with cut() and scale_fill_manual() like in this answer?
library(tidyverse)
set.seed(1)
# create dummy data
df <- expand_grid(
color = c("red", "green", "blue"),
temperature = c("cold", "neutral", "hot")
) %>% mutate(
value = unlist(map(c(0, 1, 10000), ~ rnorm(3, mean = .x)))
)
knitr::kable(df)
| color | temperature | value |
|---|---|---|
| red | cold | -0.6264538 |
| red | neutral | 0.1836433 |
| red | hot | -0.8356286 |
| green | cold | 2.5952808 |
| green | neutral | 1.3295078 |
| green | hot | 0.1795316 |
| blue | cold | 10000.4874291 |
| blue | neutral | 10000.7383247 |
| blue | hot | 10000.5757814 |
# plot with default breaks
df %>%
ggplot() +
geom_tile(aes(color, temperature, fill = value)) +
scale_fill_binned()

# plot with manual breaks
df %>%
ggplot() +
geom_tile(aes(color, temperature, fill = value)) +
scale_fill_binned(breaks = c(-10, 0, 1, 2, 1000, 5000, 10000))

Created on 2021-10-14 by the reprex package (v2.0.1)</sup