I am plotting a histogram with ggplot2 and trying to figure out how to color specific bins in another color than the others. The bins I want to color are defined by their bin edges / ranges.
Similar questions I found were asking for conditional coloring based on the original values, not the bin ranges, either for a specific value or using a threshold.
Example:
dt <- data.table(x = runif(10000))
ggplot(dt, aes(x)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", fill = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
which gives me this plot:
I defined the leftmost bin to be [0, 0.01), from there the others are calculated.
Now I want to color the following bins differently: [0, 0.01), [0.1, 0.11), [0.2, 0.21) ..., i.e. the bins starting at
> seq(0, 1, 0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
How can I do this?
EDIT: this is my desired plot: