Search code examples
rplotraster

How to keep only break in colors to septate values above zero from those below zero?


I want to plot a matrix but with continues colour no breaks in the legend

       library(colorRamps) 
       library(raster) 
       set.seed(1)
       library(raster)
       r <- raster(nrows=5, ncols=5)
       r <- setValues(r, 1:ncell(r))
      plot(r, col =  matlab.like(8)

This produce enter image description here

As you can see the legend has breaks for the colours. I want to use the same colours but without breaks, but to keep only one break which separate the orange and red colours from the light blue and blue colours please help (it is between 10 and 15 in this example). My real values are between -1 and +1 so I want to put a break at zero. above zero orang,red,etc.. below zero light blue , blue , etc. Thanks


Solution

  • It sounds like to want to mix continuous and discrete scales, e.g., to have a discrete scale with two continuous scales embedded. I'm not aware of a way to do this in R. However just changing your last line of code to

    plot(r, col =  matlab.like(25)
    

    creates something pretty close:

    Using ggplot, you have more flexibility in defining scales, but still can't combine discrete and continuous.

    library(ggplot2)
    library(colorRamps)
    
    x    <- seq(-1,1,len=5)
    y    <- seq(-1,1,len=5)
    gg   <- expand.grid(x=x,y=-y)
    gg$z <- seq(-1,1,len=25)
    ggplot(gg) + 
      geom_raster(aes(x=x,y=y,fill=z))+
      scale_fill_gradientn(colours=matlab.like(25), breaks=c(-1,0,1))