anim.plot: Create an animated plot.

Description Usage Arguments Details Examples

View source: R/anim.plots.R

Description

anim.plot

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 xy.coords.

times

a vector of times. If times is length one, there will be that many frames, equally divided over the length of x and y.

speed

animation speed.

show

if false, do not show plot; just return calls.

use.times

if TRUE, animation speed is determined by the times argument. If FALSE, animation speed is constant.

window

what window of times to show in each animation. The default, t, shows just plots from time t. To draw a plot incrementally, use window=1:t.

window.process

function to call on each window of each times. See details.

xlim, ylim, col, pch

arguments passed to plot.

labels, cex, lty, lwd

as above.

asp, xaxp, yaxp, ...

as above.

fn

function called to create each frame.

formula

a formula of the form y ~ x + time.

data

a data frame from where the values in formula should be taken.

subset

a vector specifying which rows of data to use.

Details

Each unique level of times will generate a single frame of animation. The frames will be ordered by times.

In general:

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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)

anim.plots documentation built on May 30, 2017, 12:31 a.m.