简体   繁体   中英

A function in R for plotting locations with consistent high values in multiple raster data

I have four raster files of the same extent. The pattern of low and high values differ in each raster data. I would like to plot areas in the extent (boundary) with values greater than x (where x is an integer). Can anyone help me with an r function to do this? Please find below a sample code for the raster data. In this example, let say I want to plot and identify cells with values greater 0.4 in all the four rasters. Instead of four separate images I want one image that shows cells with values greater than 4. More like overlaying the raster and identifying cells with values greater than 4 in all the images

   library(raster)

r1 <- raster(nrows = 1, ncols = 1, res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = 0.3)

rr <- lapply(1:4, function(i) setValues(r1,runif(ncell(r1))))

par(mfrow = c(2,2))
plot(rr[[1]])
plot(rr[[2]])
plot(rr[[3]])
plot(rr[[4]])

四个栅格的图

Thank you/

You can combine raster images with & . First threshold each individual plot:

r2 = lapply(rr, `>`, threshold)

And then combine them, retaining only fields which are all greater than the threshold:

summary = Reduce(`&`, r2)
plot(summary)

在此处输入图像描述

This is a super easy solution that can be easily generalized for different length of input:

myplot <- function(input,threshold){
  input <- lapply(input,function(x){
    x <- x>threshold
  })
  
  par(mfrow = c(2,2))
  plot(input[[1]])
  plot(input[[2]])
  plot(input[[3]])
  plot(input[[4]])
}

myplot(rr,0.4)

在此处输入图像描述

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