17  Animations

library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(tmap)
slo_borders = read_sf("data/slovenia/slo_border.gpkg")
slo_regions_ts = read_sf("data/slovenia/slo_regions_ts.gpkg")

17.1 Creating animations

tm = tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_polygons("gdppercap")
tm +
  tm_animate(by = "time")
tm +
  tm_animate_fast(by = "time")
tm +
  tm_animate(by = "time", fps = 5, play = "pingpong")
library(stars)
slo_tavg = read_stars("data/slovenia/slo_tavg.tif")
tm_shape(slo_tavg) +
  tm_raster() +
  tm_animate(by = "band", fps = 6)
#> [scale] tm_raster:() the data variable assigned to 'col' contains positive and negative values, so midpoint is set to 0. Set 'midpoint = NA' in 'fill.scale = tm_scale_intervals(<HERE>)' to use all visual values (e.g. colors)

17.2 Saving animations

tma1 = tm +
  tm_animate_fast(by = "time")
tmap_animation(tma1, "tma1.gif")