Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use animate() with series of levelplots in R raster

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?

like image 941
Niels DB Avatar asked Dec 07 '17 15:12

Niels DB


1 Answers

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")

enter image description here

like image 167
Christopher Stephan Avatar answered Oct 18 '22 01:10

Christopher Stephan