0

I have been attempting to animate a raster stack using tmap::tmap_animation(). Each layer covers the exact same geographic extent, but a different date. The output GIF animation is a single frame with multiple small plots.

Is it possible to create a gif animation of RasterStack using tmap?

The only workaround I have found is to:

  1. convert each layer to an SF object
  2. give each new sf polygon object an attribute 'layer' representing the RasterStack layer it was pulled from
  3. rbind the sf objects together

Once completing the above steps it is possible to plot a gif animation, but it is painfully slow.

Jackson
  • 111
  • 5

1 Answers1

3

tmap can plot rasterstacks or rasterbricks as animations directly. The simplest way is to specify tm_facets() to restrict the number of rows and columns in each frame to 1, which stops it creating small multiples, e.g.:

library(raster)
library(tmap)

a <- raster(matrix(runif(100), 10, 10))
b <- raster(matrix(runif(100), 10, 10))
c <- raster(matrix(runif(100), 10, 10))
d <- raster(matrix(runif(100), 10, 10))

my_stack <- stack(a, b, c, d)

anim <- tm_shape(my_stack) + tm_raster() + tm_facets(nrow = 1, ncol = 1)

tmap_animation(anim, "anim_file.gif")
G-Lomax
  • 61
  • 6