anim.plot {anim.plots} | R Documentation |
Create an animated plot.
Description
anim.plot
Usage
anim.plot(...)
anim.points(...)
anim.lines(...)
anim.text(...)
## Default S3 method:
anim.plot(
x,
y = NULL,
times = 1:length(x),
speed = 1,
show = TRUE,
use.times = TRUE,
window = if (identical(fn, lines)) t:(t + 1) else t,
window.process = NULL,
xlim = NULL,
ylim = NULL,
col = par("col"),
xaxp = NULL,
yaxp = NULL,
pch = par("pch"),
cex = 1,
labels = NULL,
asp = NULL,
lty = par("lty"),
lwd = par("lwd"),
fn = plot,
...
)
## S3 method for class 'formula'
anim.plot(
formula,
data = parent.frame(),
subset = NULL,
fn = plot,
window = t,
...
)
## Default S3 method:
anim.points(...)
## Default S3 method:
anim.lines(...)
## Default S3 method:
anim.text(...)
anim.symbols(...)
## S3 method for class 'formula'
anim.points(formula, ...)
## S3 method for class 'formula'
anim.lines(formula, ...)
## S3 method for class 'formula'
anim.text(formula, ...)
Arguments
x , y |
vectors of x and y coordinates. These can be passed in any way
accepted by |
times |
a vector of times. If |
speed |
animation speed. |
show |
if false, do not show plot; just return calls. |
use.times |
if |
window |
what window of times to show in each animation. The default,
|
window.process |
function to call on each window of each times. See details. |
xlim , ylim , col , pch |
arguments passed to |
labels , cex , lty , lwd |
as above. |
asp , xaxp , yaxp , ... |
as above. |
fn |
function called to create each frame. |
formula |
a |
data |
a data frame from where the values in |
subset |
a vector specifying which rows of |
Details
Each unique level of times
will generate a single frame of animation.
The frames will be ordered by times
.
In general:
Parameters that apply to each point of the plot, such as
xlim, ylim, col, pch, labels
andcex
, can be passed as vectors which will be recycled tolength(times)
.Parameters that apply to the plot as a whole, and always have length 1, such as
xlab
andmain
, can be passed as vectors and will be recycled to the number of frames.Parameters that apply to the plot as a whole, and can have length > 1, such as
xlim
andylim
, can be passed as vectors or matrices. If vectors, the same vector will be passed to every frame. If matrices, columni
will be passed to thei
'th frame.
window.process
should be a function which takes
two arguments: a list of potential arguments for the underlying
call to plot
, and a vector of times. The function should return
the list of arguments after modification. This allows e.g. drawing
"trails" of plot points. See the example
Examples
x <- rep(1:100/10, 10)
times <- rep(1:10, each=100)
y <- sin(x*times/4)
anim.plot(x,y,times, type="l", col="orange", lwd=2)
## changing colours - a per-point parameter
anim.plot(x,y,times, ylab="Sine wave", type="p", col=rainbow(100)[x *10])
## changing line width - a whole-plot parameter
anim.plot(x, y, times, lwd=1:10, type="l")
## times as a single number
anim.plot(1:10, 1:10, times=5)
## incremental plot
anim.plot(1:10, 1:10, window=1:t)
## moving window
anim.plot(1:10, 1:10, window=(t-2):t)
## Formula interface
ChickWeight$chn <- as.numeric(as.factor(ChickWeight$Chick))
tmp <- anim.plot(weight ~ chn + Time, data=ChickWeight, col=as.numeric(Diet),
pch=as.numeric(Diet), speed=3)
# adding extra arguments:
replay(tmp, after=legend("topleft", legend=paste("Diet", 1:4), pch=1:4, col=1:4))
## Zooming in:
x <- rnorm(4000); y<- rnorm(4000)
x <- rep(x, 10); y <- rep(y, 10)
xlims <- 4*2^(-(1:10/10))
ylims <- xlims <- rbind(xlims, -xlims)
anim.plot(x, y, times=10, speed=5, xlim=xlims, ylim=ylims,
col=rgb(0,0,0,.3), pch=19)
## window.process to create a faded "trail":
anim.plot(1:50, 1:50, speed=12, window=t:(t+5),
window.process=function(args, times){
times <- times - min(times)
alpha <- times/max(times)
alpha[is.na(alpha)] <- 1
args$col <- rgb(0,0,0, alpha)
return(args)
})
## gapminder plot:
pl <- palette(adjustcolor(rainbow(23), 1, .6, .6, .6,
offset=c(0,0,0,-0.1)))
anim.plot(lifex ~ GDP + year, data=gm_data, log="x",
cex=sqrt(pop)*0.0004, pch=19, col=region, xlab="GDP",
ylab="Life expectancy", speed=10, subset=year > 1850 & !year %% 5)
palette(pl)
## Not run:
## Earthquakes this week
if (require('maps')) {
eq = read.table(
file="http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt",
fill=TRUE, sep=",", header=TRUE)
eq$time <- as.numeric(strptime(eq$Datetime, "%A, %B %d, %Y %X UTC"))
eq <- eq[-1,]
map('world')
maxdepth <- max(max(eq$Depth), 200)
tmp <- anim.points(Lat ~ Lon + time, data=eq, cex=Magnitude, col=rgb(
1-Depth/maxdepth, 0, Depth/maxdepth,.7), pch=19, speed=3600*12,
show=FALSE)
replay(tmp, before=map('world', fill=TRUE, col="wheat"))
}
## Minard's plot
if (require('maps')) {
map('world', xlim=c(22, 40), ylim=c(52,58))
title("March of the Grande Armee on Moscow")
points(cities$long, cities$lat, pch=18)
text(cities$long, cities$lat, labels=cities$city, pos=4, cex=.7)
with(troops[troops$group==1,], anim.lines(x=long,
y=lat, window=t:(t+1), speed=3, lwd=survivors/10000))
}
## End(Not run)