Spatio-Temporal Animation of an Epidemic
epidata_animate.RdFunction for the animation of epidemic data, i.e. objects inheriting from
class "epidata". This only works with 1- or 2-dimensional coordinates
and is not useful if some individuals share the same coordinates
(overlapping). There are two types of animation, see argument
time.spacing. Besides the direct plotting in the R session, it is
also possible to generate a sequence of graphics files to create animations
outside R.
Usage
# S3 method for class 'summary.epidata'
animate(object, main = "An animation of the epidemic",
pch = 19, col = c(3, 2, gray(0.6)), time.spacing = NULL,
sleep = quote(5/.nTimes), legend.opts = list(), timer.opts = list(),
end = NULL, generate.snapshots = NULL, ...)
# S3 method for class 'epidata'
animate(object, ...)Arguments
- object
an object inheriting from class
"epidata"or"summary.epidata". In the former case, its summary is calculated and the function continues as in the latter case, passing all...arguments to thesummary.epidatamethod.- main
a main title for the plot, see also
title.- pch, col
vectors of length 3 specifying the point symbols and colors for susceptible, infectious and removed individuals (in this order). The vectors are recycled if necessary. By default, susceptible individuals are marked as filled green circles, infectious individuals as filled red circles and removed individuals as filled gray circles. Note that the symbols are iteratively drawn (overlaid) in the same plotting region as time proceeds. For information about the possible values of
pchandcol, see the help pages ofpointsandpar, respectively.- time.spacing
time interval for the animation steps. If
NULL(the default), the events are plotted one by one with pauses ofsleepseconds. Thus, it is just the ordering of the events, which is shown. To plot the appearance of events proportionally to the exact time line,time.spacingcan be set to a numeric value indicating the period of time between consecutive plots. Then, for each time point inseq(0, end, by = time.spacing)the current state of the epidemic can be seen and an additional timer indicates the current time (seetimer.optsbelow). The argumentsleepwill be the artificial pause in seconds between two of those time points.- sleep
time in seconds to
Sys.sleepbefore the next plotting event. By default, each artificial pause is of length5/.nTimesseconds, where.nTimesis the number of events (infections and removals) of the epidemic, which is evaluated in the function body. Thus, fortime.spacing = NULLthe animation has a duration of approximately 5 seconds. In the other case,sleepis the duration of the artificial pause between two time points. Note thatsleepis ignored on non-interactive devices (seedev.interactive)- legend.opts
either a list of arguments passed to the
legendfunction orNULL(orNA), in which case no legend will be plotted. All necessary arguments have sensible defaults and need not be specified, i.e.x:"topright"legend:c("susceptible", "infectious", "removed")pch:same as argument
pchof the main functioncol:same as argument
colof the main function
- timer.opts
either a list of arguments passed to the
legendfunction orNULL(orNA), in which case no timer will be plotted. All necessary arguments have sensible defaults and need not be specified, i.e.x:"bottomright"title:"time"box.lty:0adj:c(0.5,0.5)inset:0.01bg:"white"
Note that the argument
legend, which is the current time of the animation, can not be modified.- end
ending time of the animation in case of
time.spacingnot beingNULL. By default (NULL), time stops after the last event.- generate.snapshots
By default (
NULL), the animation is not saved to image files but only shown on the on-screen device. In order to print to files,time.spacingmust not beNULL, a screen device must be available, and there are two options:
If the framework of the animation package should be used, i.e. theanimate-call is passed as theexprargument to one of thesave*functions of the animation package, then setgenerate.snapshots = img.name, whereimg.nameis the base name for the generated images (the same as passed to thesave*function). The path and format (type, width, height) for the generated images is derived fromani.options. See the last example below.
Alternatively,generate.snapshotsmay be a list of arguments passed to the functiondev.print, which then is executed at each time point of the grid defined bytime.spacing. Essentially, this is used for saving the produced snapshots to files, e.g.generate.snapshots = list(device=pdf, file=quote(paste("epidemic_",sprintf(form,tp),".pdf", sep="")))will store the animation steps in pdf-files in the current working directory, where the file names each end with the time point represented by the corresponding plot. Because the variables
tpandformshould only be evaluated inside the function thefileargument isquoted. Alternatively, the file name could also make use of the internal plot indexi, e.g., usefile=quote(paste("epidemic",i,".pdf",sep="")).- ...
further graphical parameters passed to the basic call of
plot, e.g.las,cex.axis(etc.) andmgp.
See also
summary.epidata for the data, on which the plot is based.
plot.epidata for plotting the evolution of an epidemic by
the numbers of susceptible, infectious and removed individuals.
The contributed R package animation.
Examples
data("hagelloch")
(s <- summary(hagelloch))
# plot the ordering of the events only
animate(s) # or: animate(hagelloch)
# with timer (animate only up to t=10)
animate(s, time.spacing=0.1, end=10, sleep=0.01,
legend.opts=list(x="topleft"))
# Such an animation can be saved in various ways using tools of
# the animation package, e.g., saveHTML()
if (interactive() && require("animation")) {
oldwd <- setwd(tempdir()) # to not clutter up the current working dir
saveHTML({
par(bg="white") # default "transparent" is grey in some browsers
animate(s, time.spacing=1, sleep=0, legend.opts=list(x="topleft"),
generate.snapshots="epiani")
}, use.dev=FALSE, img.name="epiani", ani.width=600, interval=0.5)
setwd(oldwd)
}