Create discrete color bar with varying interval widths and no spacing between legend levels

colors, ggplot2, r

Solution

I think the following answer is sufficiently different to merit a second answer. ggplot2 has massively changed in the last 2 years (!), and there are now new functions such as `scale_..._binned`, and specific gradient creating functions such as `scale_..._fermenter`

This has made the creation of a discrete gradient bar fairly straight forward.

For a "full separator" instead of ticks, see user teunbrands post.

library(ggplot2)

ggplot(iris, aes(Sepal.Length, y = Sepal.Width, fill = Petal.Length))+
  geom_point(shape = 21) +
  scale_fill_fermenter(breaks = c(1:3,5,7), palette = "Reds") + 
  guides(fill = guide_colorbar(
    ticks = TRUE, 
    even.steps = FALSE,
    frame.linewidth = 0.55, 
    frame.colour = "black", 
    ticks.colour = "black",
    ticks.linewidth = 0.3)) +
  theme(legend.position = "bottom")

Problem

I'd like to reproduce this color scale in `ggplot2`: (Source) In the past I have found that creating discrete color scales with labels in-between in `ggplot2` can be tricky. Can this be accomplished at all? A similar, but not completely identical question I have recently posed is this one.

Original source

Related problems