require(ggmap)
data(transfers)
## First extract all source and destination from the dataset
root <- unique(c(transfers$source, transfers$destination))
## For this example, generate a random coordinate for each
## holding. Note that some coordinates might end up in water,
## but the coordinates are only for demonstrating the animation.
ngen <- length(root)
set.seed(123)
lon_min <- 13
lon_max <- 17
lat_min <- 56
lat_max <- 63
lon <- lon_min + runif(ngen) * (lon_max - lon_min)
lat <- lat_min + runif(ngen) * (lat_max - lat_min)
coords <- data.frame(id=root, lon, lat)
## Fetch a map over Sweden
sweden <- get_map('Sweden', zoom=5)
## Select a subset of all movements to visualize
i <- sample(seq_len(nrow(transfers)), 100, replace=FALSE)
## Perform the animation and view the movements aggregated by week
## in a web-browser.
Animate(transfers[i,], coords, sweden, "week")
Run the code above in your browser using DataLab