hyat
hyat

Reputation: 1057

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

Upvotes: 0

Views: 650

Answers (1)

koekenbakker
koekenbakker

Reputation: 3604

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)

Upvotes: 1

Related Questions