Search code examples
rmemoryplotrasterr-raster

Plot raster nonlinear stretch. Any other way than raster calc?


I am trying to produce raster plots of whole world with bioclimatic variables from Chelsa or WorldClim.

I am not able to produce nice maps especially while working with precipitation data having large range of values. Trying any colour scale lead to almost mono colour maps because of just few spots with very high precipitation and the rest of world with relatively low.

Is there some elegant way how to stretch the colours while plotting maps? I prefer nonlinear stretch using log or standart deviation. Is there any way without need of compute and save completely new raster? I have tried function form raster package stretch but my PC was not able to allocate data during run of function into PC memory.


Solution

  • For map composing, you don't need to create a new raster, just change legend color range.

    library(raster)
    library(classInt)
    library(rasterVis)
    library(RColorBrewer)
    
    r <-getData('worldclim', var='bio', res=10)
    
    levelplot(r[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')),margin=FALSE,main ='Normal breaks')
    

    enter image description here

    Using classIntervals() function from classInt package, you can compute new breaks.

    breaks <- classIntervals(r[[12]][!is.na(r[[12]])], n = 50, style = "quantile")
    
    levelplot(r[[12]], at = breaks$brks, col.regions=colorRampPalette(brewer.pal(9, 'Blues')),margin=FALSE,main ='Quantile breaks')
    

    enter image description here