Image labels in levelplot animation R

169 Views Asked by At

Using the example from the question here , How can one possibly useyears as title for each image?

Example:

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,main=noquote(paste("",i)))
  plot(l)
 }
}, interval=0.2, movie.name="animation.gif")

The line main=noquote(paste("",i)) will label each image in the animation as 1,2,3, etc... However, I would like to use 1919, 1920, 1921, etc.. instead. Is another for loop needed to generate 1919, 1920, 1921, etc..?

The following do not work:

main=paste(1919,i)
main=paste(1919:1923,i)

My actual rasterbrick has 100 layers

Any suggestions for using the year as title for each image in the animation?

1

There are 1 best solutions below

0
On

This worked for me after a few thoughts:

main=paste(" ", 1918+i, sep='')

Assuming my data starts in 1919, sequential years with a step of 1 will be added to each image.