I am trying to change the breaks of a raster in R that is shown with a continuous scale. However, changing just the breaks also changes the colour scale. I am not sure how I can fix this?

r <- raster(xmn=1, xmx=5, ymn=1, ymx=5, nrows=5, ncols=5)
r[] <- 1:length(r)

Normal plot:


enter image description here

Now just change the breaks, say to 21 instead of 20:

plot(r, breaks=c(0, 5, 10, 15, 21, 25))

enter image description here

Changing breaks also changed the colour!? Why? My guess is that now plot.raster() uses the 5 (or 25?) first colour of the colour ramp, which are all pretty much the same? The issues seems that it handles this now as a categorical colour palette? Indeed, giving a custom ramp equally spaced with 25 colours, it seems it only uses the first 6?

cols <- rev(terrain.colors(255))
plot(r, breaks=c(0, 5, 10, 15, 21, 25), 

cols <- rev(terrain.colors(255))
  • This answer could help you
    – aldo_tapia
    Sep 14, 2017 at 18:44
  • 1
    Use zlim = c(0, 25) instead (for a linear sequence), otherwise set the colours per interval as well.
    – mdsumner
    Sep 15, 2017 at 10:25
  • How exactly would you use it @mdsumner ? zlim and breaks together doesn't seem to work? Thanks!
    – Matifou
    Sep 15, 2017 at 17:25
  • Tney are not meant to work together (though I also see now that zlim is not documented, except for examples in ?plot). If you set breaks, give a col for each interval. If you only want to set the colours and an absolute but linear breaks range, use zlim+col. Your question isn't currently clear about what you want, so try plot(r, zlim = c(0, 25)) and please build from that in the q
    – mdsumner
    Sep 15, 2017 at 23:42

1 Answer 1


The rasterVis package (https://oscarperpinan.github.io/rastervis/) is the most effective and easiest way to plot raster data in R

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.