Use animate() with series of levelplots in R raster

北城余情 提交于 2019-12-07 07:15:13

问题


I have a time series of 25 yearly land cover rasters. As this is categorical data, I use levelplot(inputRaster) (part of the rasterVis library) to plot a single raster. However, I would like to sequentially plot the yearly rasters, as the animate function of the raster library does. When I use

rasStack <- stack(listOfRasters) animate(rasStack)

The result does not have a categorical legend. So in short: how can I combine the functionalities of levelplot and animate?


回答1:


Function animate only accepts raster objects as input. You can try saveGIF to animate levelplots:

library(raster)
library(rasterVis)
library(animation)
library(classInt)

r <- raster(ncol=40, nrow=20)
r[] <- rnorm(n=ncell(r))
s <- stack(x=c(r, r*r, r*r*r, r*r*r*r))

classes <- classIntervals(values(r), n=5, style="fisher", precision = 3)
brks <- classes$brks
brks <- round(brks, 2)

saveGIF({
  for(i in c(1:nlayers(s))){
    l <- levelplot(s[[i]], colorkey=list(at=brks, labels=c(as.character(brks))), margin=FALSE)
    plot(l)
  }
}, interval=0.2, movie.name="animation.gif")



来源:https://stackoverflow.com/questions/47698349/use-animate-with-series-of-levelplots-in-r-raster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!