使用 spplot 在单页中绘制多个 shapefile

Plot multiple shapefiles in single page using spplot

这就是我绘制多个栅格的方式

library(raster)
x <- raster::getData('worldclim', var='tmin', res = 10)
var.list <- c("tmin1","tmin2","tmin3","tmin4")

ras.stack <- stack()

for(i in var.list){

  stack.list <- stack(stack.list, x[[paste0(i)]])
}

spplot(stack.list)

我想对具有共同属性的 4 个形状文件执行相同的操作 叫做 "mean.value"

fra <- raster::getData('GADM',country = 'FRA', level = 2)
shp.stack <- stack()

for(i in 1:4){
  mean.value <- data.frame(NAME_2  = fra@data$NAME_2, sample(1:200, 96))

  my.shp <- merge(fra, mean.value, by = 'NAME_2') 
  shp.stack <- stack(shp.stack, my.shp)
}

Error in sapply(x, fromDisk) & sapply(x, inMemory) : operations are possible only for numeric, logical or complex types

我该如何解决?

您必须先将 SpatialPolygonsDataFrame 转换为 栅格 对象,才能堆叠它。您还可以转换为 SpatialGrid*、SpatialPixels* - 基于 raster::stack.

手册的对象

所以你的第二个代码会变成这样:

library(raster)
fra <- raster::getData('GADM',country = 'FRA', level = 2)
shp.stack <- stack()
for(i in 1:4){
  mean.value <- data.frame(NAME_2  = fra@data$NAME_2, sample(1:200, 96))
  my.shp <- raster::merge(fra, mean.value, by = 'NAME_2')

  r <- raster(ncol=180, nrow=180)
  extent(r) <- extent(my.shp)
  rp <- rasterize(x = my.shp, y = r)

  shp.stack <- raster::stack(shp.stack, rp)
}

plot(shp.stack)