I have a raster and I want to plot it using image()
. So far, I plotted the raster with my own color scale col
.
## read the libraries
library(raster)
library(fields)
library(grDevices)
##random raster object
set.seed(1)
r <- raster(ncol=5, nrow=5)
r[] <- rnorm(n=ncell(r),mean=2)
col = colorRampPalette(c("darkred","red","lightskyblue","blue","blue4"))(20)
image(r, xaxs="i", yaxs="i", col= rev(col))
This looks like
Now, I want to plot the all the values higher than the value 2 as "darkred" (initial color from my color scale)
I found a similar post and tried the same
zlim=2
newcol = ifelse(raster(r) >= zlim,"darkred",col)
image(r, xaxs="i", yaxs="i", col= newcol)
However, I got some error message. It would be helpful if anyone can help me with this.