-1

This is how I plot multiple raster

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)

I want to do the same for 4 shape files which have a common attribute called "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

How can I fix it?

SeGa
  • 9,454
  • 3
  • 31
  • 70
89_Simple
  • 3,393
  • 3
  • 39
  • 94
  • I am not able to use spplot with your first example. With normal `plot` it works, but I also had to change `stack.list` to `ras.stack` in the `stack` function. Otherwise I'll get the error `object 'stack.list' not found`. Why do you do `x[[paste0(i)]]` instead of just `x[[i]]`? And what exactly are you trying to do/plot? – SeGa Jan 22 '19 at 18:08

1 Answers1

0

You have to transform the SpatialPolygonsDataFrame to a raster object first, to be able to stack it. You could also transform to SpatialGrid*, SpatialPixels* - objects based on the manual of raster::stack.

So your second code would become something like this:

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)

enter image description here

SeGa
  • 9,454
  • 3
  • 31
  • 70