Search code examples
rplotggplot2heatmap

easiest way to discretize continuous scales for ggplot2 color scales?


Suppose I have this plot:

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + scale_colour_gradient()

what is the correct way to discretize the color scale, like the plot shown below the accepted answer here (gradient breaks in a ggplot stat_bin2d plot)?

ggplot correctly recognizes discrete values and uses discrete scales for these, but my question is if you have continuous data and you want a discrete colour bar for it (with each square corresponding to a value, and squares colored in a gradient still), what is the best way to do it? Should the discretizing/binning happen outside of ggplot and get put in the dataframe as a separate discrete-valued column, or is there a way to do it within ggplot? an example of what I'm looking for is similar to the scale shown here: enter image description here

except I'm plotting a scatter plot and not something like geom_tile/heatmap.

thanks.


Solution

  • The solution is slightly complicated, because you want a discrete scale. Otherwise you could probably simply use round.

    library(ggplot2)
    
    bincol <- function(x,low,medium,high) {
      breaks <- function(x) pretty(range(x), n = nclass.Sturges(x), min.n = 1)
    
      colfunc <- colorRampPalette(c(low, medium, high))
    
      binned <- cut(x,breaks(x))
    
      res <- colfunc(length(unique(binned)))[as.integer(binned)]
      names(res) <- as.character(binned)
      res
    }
    
    labels <- unique(names(bincol(iris$Sepal.Length,"blue","yellow","red")))
    breaks <- unique(bincol(iris$Sepal.Length,"blue","yellow","red"))
    breaks <- breaks[order(labels,decreasing = TRUE)]
    labels <- labels[order(labels,decreasing = TRUE)]
    
    
    ggplot(iris) + 
      geom_point(aes(x=Sepal.Width, y=Sepal.Length,
                     colour=bincol(Sepal.Length,"blue","yellow","red")), size=4) +
      scale_color_identity("Sepal.Length", labels=labels, 
                           breaks=breaks, guide="legend")
    

    enter image description here