maximusdooku maximusdooku - 1 year ago 82
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)

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



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


I can see the figure for individual geoTiff files:

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

One of the files:

Answer Source

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.



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) )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download