How to present some regions in raster (matrix) with conditions from another raster?

StackOverflow https://stackoverflow.com/questions/20611945

  •  02-09-2022
  •  | 
  •  

문제

I have two matrix r and r2. I would like to plot r , this simply can be done using

       plot (r)

But I would like to mark all values higher than 20 in r2 in the plot of r. a reproducible example:

   library(raster)
  r <- raster(nrows=10, ncols=10)
  r <- setValues(r, 1:ncell(r))
  r2 <- raster(nrows=10, ncols=10)
 r2 <- setValues(r2, 1:ncell(r))
 plot(r)

This will poroduce this:

enter image description here

I would like to mark in this plot all areas where r2 higher than 20 in red for example.Thanks for your ideas

도움이 되었습니까?

해결책

You can do this for example:

set.seed(1)
library(raster)
r <- raster(nrows=5, ncols=5)
r <- setValues(r, 1:ncell(r))
r2 <- raster(nrows=5, ncols=5)
r2 <- setValues(r2, round(runif(ncell(r),10,20)),2)
## 100 here as a value that don't exist in range of values(r)
## and it gives the blue value
r[r2<15] <- '100'
## I plot the 3 to see check the result
plot(stack(r,r2,r2<15))

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top