Skip to contents

The animate-method for sts objects iterates over time points, plotting maps of the current|cumulative counts|incidence via stsplot_space, optionally including a time series chart below the map to track the epidemic curve. It is worth using functionality of the animation package (e.g., saveHTML) to directly export the animation into a useful format.


# S3 method for class 'sts'
animate(object, tps = NULL, cumulative = FALSE,
        population = NULL, at = 10, ...,
        timeplot = list(pos = 1, size = 0.3, fill = TRUE),
        sleep = 0.5, verbose = interactive(), draw = TRUE)



an object of class "sts" or a matrix of counts, i.e., observed(stsObj), where especially colnames(x) have to be contained in row.names(map). If a matrix, the map object has to be provided explicitly (as part of ...).


a numeric vector of one or more time points at which to plot the map. The default tps=NULL means the whole time period 1:nrow(object).


logical specifying if the cumulative counts/incidence over time should be plotted. The cumulative incidence is relative to the population from the first time point tps[1] throughout the whole animation, while cumulative=FALSE computes the incidence from the current population numbers.


arguments for stsplot_space.


if a list and package gridExtra is available, a time series chart of the counts along the selected time points tps will be plotted next to the map. The list elements determine both the positioning of this plot (pos, size, and fill) and its appearance. The default pos=1 and size=0.3 arguments put the time series plot below the map, using 30% of the total plot height. The logical value fill indicates whether to make the panel as big as possible (default: TRUE). An alternative to fill=FALSE is to manually specify an aspect (ratio) value in timeplot. Other list elements are arguments for the internal (and currently undocumented) function stsplot_timeSimple. For example, inactive and active are lists of graphical parameters (e.g., col) determining the appearance of the bars (e.g., default color is grey when inactive and black when active), and the boolean as.Date determines whether dates should be put on the x-axis (instead of the tps indexes).


time to wait (Sys.sleep) between subsequent snapshots (only if dev.interactive), in seconds.


logical indicating if a txtProgressBar should be shown during generation of the animation -- which may take a while. Default is to do so in interactive sessions.


logical indicating if the produced plots at each time point should be drawn directly (the default) or not. The setting draw = FALSE is useful if one would like to manually arrange the plots, which are always returned invisibly in a list of length length(tps).


(invisibly) a list of the length(tps) sequential plot objects. These are of class "gtable" (from gtable) if the timeplot is included, otherwise of class



Sebastian Meyer

See also

the other plot types documented in stsplot for static time series plots and maps.



## animate the weekly counts of measles (during weeks 12-16 only, for speed)
if (interactive() && require("animation")) {
    oldwd <- setwd(tempdir())  # to not clutter up the current working dir
    saveHTML(animate(measlesWeserEms, tps=12:16),
             title="Evolution of the measles epidemic in the Weser-Ems region",
             ani.width=500, ani.height=600)

if (FALSE) {
## animate the weekly incidence of measles (per 100'000 inhabitants),
## and label the time series plot with dates in a specified format
animate(measlesWeserEms, tps=12:16,
        population = measlesWeserEms@map$POPULATION / 100000,
        timeplot = list(as.Date = TRUE,
                        scales = list(x = list(format = "%G/%V"))))