ggplot scale color gradient to range outside of data range

50,925

It's very important to remember that in ggplot, breaks will basically never change the scale itself. It will only change what is displayed in the guide or legend.

You should be changing the scale's limits instead:

ggplot(data=t, aes(x=x, y=y)) +
  geom_tile(aes(fill=z)) +
  scale_fill_gradientn(limits = c(-3,3),
  colours=c("navyblue", "darkmagenta", "darkorange1"),
  breaks=b, labels=format(b))

And now if you want the breaks that appear in the legend to extend further, you can change them to set where the tick marks appear.

A good analogy to keep in mind is always the regular x and y axes. Setting "breaks" there will just change where the tick marks appear. If you want to alter the extent of the x or y axes, you'd typically change a setting like their "limits".

Share:
50,925
metasequoia
Author by

metasequoia

I am a data scientist with a background in geoinformatics and agriculture. My work combines remote sensing, biophysical modeling, and machine learning to better understand human-crop-climate interactions. I develop scientific software with python, R, and open-source geospatial tools to achieve that end.

Updated on July 09, 2022

Comments

  • metasequoia
    metasequoia almost 2 years

    I am looking for a way to stretch a color gradient between two values and label the legend, regardless of the range of data values in the dataset. Essentially, is there a functional equivalent to ylim() for color gradients?

    Given code which plots a z value typically between -1 and 1, I can plot and label a gradient if the breaks are within the data range:

    library(ggplot2)
    
    #generator from http://docs.ggplot2.org/current/geom_tile.html
    pp <- function (n, r = 4) { 
      x <- seq(-r * pi, r * pi, len = n)
      df <- expand.grid(x = x, y = x)
      df$r <- sqrt(df$x^2 + df$y^2)
      df$z <- cos(df$r^2) * exp(-df$r / 6)
      return(df)
    }
    
    t <- pp(30)
    summary(t)
    b <- c(-.5, 0, .5)
    colors <- c('navyblue', 'darkmagenta', 'darkorange1')
    p <- ggplot(data = t, aes(x = x, y = y))+
      geom_tile(aes(fill = z))+
      scale_fill_gradientn(colors = colors, breaks = b, labels = format(b))
    ggsave(plot = p, filename = <somefile.png>, height = 3, width = 4)
    

    graham jeffries ggplot scale fill example

    But when I change the breaks to values outside of the observed range, the gradient coloring doesn't seem to adjust and the gradient labels don't appear.

    b <- c(-3, 0, 3)
    

    graham jeffries ggplot scale fill example 2

  • MartinT
    MartinT over 7 years
    I think I am trying to achieve something similar, but failing with this solution. I have a number of datasets where the values are in limited ranges, but theoretically they can range between 0 and -100 (dB). doing scale_fill_gradient(low="white", high="red") is fine, but does not let me compare visually the datasets. scale_fill_gradient(low="white", high="red",limits = c(0,-100),guide = "legend",limits = c(0,-100),breaks=b,labels=format(c(0,-100)) does not produce the legend. I am at a loss.
  • owlstone
    owlstone almost 4 years
    Thanks! This works perfectly when I set an upper limit to my tile plot. However, when I try to set a specified lower limit (i.e. any value other than "NA") R provides me with the following error: Error in UseMethod("depth") : no applicable method for 'depth' applied to an object of class "NULL". Update: I am unable to reproduce this error, and things seem to be working now!