rggplot2geom-raster

Changing color ramp in geom_raster to better display outliers


I have a dataset which has a handful of outliers that I'm plotting as a raster in ggplot. Unmodified color schemes tend to highlight these outliers at the expense of showing the distribution of most of the data. Setting limits helps, but values above the specified limits are lost. Is there a way to make values above limits plot at the end of the color ramp?

Here's an example from the faithful dataset. It's not a perfect analogy to my data, since there's no outliers causing problems, but it shows my question.

ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_raster(aes(fill = density))+
  scale_fill_gradientn(colours = 
  c("red", "orange", "yellow", "green","blue","purple","white"))

Let's say I want focus less on the higher values. Perhaps there a better way to do this rather than setting limits? With limits, I loose the data above the maximum (0.03, here).

ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))+
scale_fill_gradientn(limits = c(0,.03),
                   colours = c("red","orange", "yellow", "green","blue",
                               "purple","white"))

Ideally, I'd like to plot this like in GIS, where values above my limit would be set to the max value of the color ramp. I could always hack the data to for it display this, but I'm hoping there's a more elegant solution within ggplot.


Solution

  • You can use na.value to define the color of outliers, and match outlier color to the color for the maximum value.

    ggplot(faithfuld, aes(waiting, eruptions)) +
      geom_raster(aes(fill = density))+
      scale_fill_gradientn(limits = c(0,.03),
                           na.value = "white", #na.value color same as color for max value
                           colours = c("red","orange", "yellow", "green","blue",
                                       "purple","white"))
    

    The one downside of this is that the scale still displays 0.03 for white, although it should read >0.03 to be accurate. You may want to edit your scale to make the labels accurate.