maximusdooku maximusdooku - 1 month ago 6
R Question

How can I stack and display rasters?

I have 12 raster files in a folder on which I want to use the levelplot of RasterVis.

kpacks <- c('tiff','rgdal','raster','sp','rasterVis')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
options(max.print=5.5E5)

#World data
wlist <- list.files(pattern = "\\.tif$", include.dirs = TRUE)
s <- lapply(wlist, stack)

levelplot(s)


Error:

Error in UseMethod("levelplot") :
no applicable method for 'levelplot' applied to an object of class "list"


Note:

I can see the figure for individual geoTiff files:

levelplot(s[[1]]), for example


One of the files: https://www.dropbox.com/s/ank4uxjbjk3chaz/new_conus.tif?dl=0

Answer

You do not need to coerce the rasters to a list object, which is what is causing you issues, or use lapply for plotting multiple rasters with levelplot. Use stack or brick for reading your data and then just pass the object to levelplot.

Example

library(raster)
library(rasterVis)

s <- stack(system.file("external/rlogo.grd", package="raster")) 
levelplot(s, contour=TRUE)

With your code

s <- stack( list.files(pattern = "\\.tif$", include.dirs = TRUE) )
levelplot(s)