ggplot scale color gradient to range outside of data range

前端 未结 1 1275
盖世英雄少女心
盖世英雄少女心 2020-12-03 04:22

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 fu

相关标签:
1条回答
  • 2020-12-03 05:14

    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".

    0 讨论(0)
提交回复
热议问题