简体   繁体   中英

How give certain number in a raster a different colour (or shape) in R?

I have a raster that has this value 9999 for several pixels. I want to give these pixels certain colour before I plot the the whole raster. So the legend does not take into account this value

  Library(raster)
  filename <- system.file("external/test.grd", package="raster")
  r <- raster(filename)
  plot(r) ### normal plot which takes into account all pixels

  r[r>1000]=9999 

   plot(r)

here the legend (plot) should not take into account 9999 and instead give this value specified colour (or shape) and plot r normally. the legend of 9999 could be separated as well

You can create a copy of the raster without the 9999 cells and one with only the 9999 cells and overlay them:

library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
r[r>1000]=9999 

# raster without 9999
r2 = reclassify(r, matrix(c(1000, Inf, NA), ncol=3))
plot(r2, colNA = NA)

# raster with only 9999
r3 = reclassify(r, matrix(c(-Inf, 1000, NA, 1000, Inf, 9999), ncol=3, byrow=T))
plot(r3, add=T, col='black', colNA = NA, legend=F)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM