R ggplot2 - How do I specify out of bounds values' colour

后端 未结 2 1008
遇见更好的自我
遇见更好的自我 2020-11-30 04:40

In the plot generated by the following code I would like to alter the colours so that all values < 0.6 are the same as the \"low\" colour and all values greater than 1 ar

相关标签:
2条回答
  • 2020-11-30 05:21

    Try changing geom_tile to below:

      geom_tile(aes(fill = ifelse(value<0.6,min(m$value,na.rm=TRUE),
                                  ifelse(value>1,max(m$value,na.rm=TRUE),
                                         value))))
    

    EDIT: Then don't use min max, so we will get 0.6-1 range:

    geom_tile(aes(fill = ifelse(value<0.6,0.6,
                                ifelse(value>1,1,
                                       value))))
    
    0 讨论(0)
  • 2020-11-30 05:36

    As you said youself, you want the oob argument in the scale_fill_gradient. To clamp values, you can use squish from the scales package (scales is installed when ggplot2 is installed):

    library(scales)
    

    and later

    scale_fill_gradient(low = "red", high = "green", limits=c(0.6, 1), oob=squish)
    
    0 讨论(0)
提交回复
热议问题