4

I have a set of data which contains around 150,000 observations of 800 subjects. Each observation has: subject ID, latitude, longitude, and the time that the subject was at those coordinates. The data covers a 24-hour period.

If I plot all the data at once I just get a blob. Is anyone able to give me some tips as to how I can animate this data so that I can observe the paths of the subjects as a function of time?

I've read the spacetime vignette but I'm not entirely sure it will do what I want. At this point I'm spending a whole lot of time googling but not really coming up with anything that meets my needs.

Any tips and pointers greatly appreciated!

4

2 回答 2

3

Here my first use of animation package. It was easier than I anticipated and especially the saveHTML is really amazing. Here my scenario(even I think that my R-code will be clearer:)

  1. I generate some data
  2. I plot a basic plot for all persons as a background plot.
  3. I reshape data to get to a wide format in a way I can plot an arrow between present and next position for each person.
  4. I loop over hours , to generate many plots. I put the llop within the powerful saveHTML function.
  5. You get a html file with a nice animation. I show here one intermediate plot.

enter image description here

Here my code:

library(animation)
library(ggplot2)      
library(grid)
## creating some data of hours 
N.hour <- 24
dat <- data.frame(person=rep(paste0('p',1:3),N.hour),
                  lat=sample(1:10,3*N.hour,rep=TRUE),
                  long=sample(1:10,3*N.hour,rep=TRUE),
                  time=rep(1:N.hour,each=3))


# the base plot with
base <- ggplot() + 
  geom_point(data=dat,aes(x=lat, y=long,colour = person),
             size=5)+ theme(legend.position = "none") 


## reshape data to lat and long formats

library(plyr)
dat.segs <- ddply(dat,.(person),function(x){
  dd <- do.call(rbind,
           lapply(seq(N.hour-1),
              function(y)c(y,x[x$time %in% c(y,y+1),]$lat,
                   x[x$time %in% c(y,y+1),]$long)))
  dd

})
colnames(dat.segs) <- c('person','path','x1','x2','y1','y2')

# a function to create the animation
oopt <- ani.options(interval = 0.5)
saveHTML({
  print(base)
  interval = ani.options("interval")
  for(hour in  seq(N.hour-1)){
    # a segment for each time
    tn <- geom_segment(aes(x= x1, y= y1, xend = x2,
                        yend = y2,colour = person), 
                       arrow = arrow(), inherit.aes = FALSE,
                       data =subset(dat.segs,path==hour))

    print(base <- base + tn)
    ani.pause()
  }
}, img.name = "plots", imgdir = "plots_dir", 
   htmlfile = "random.html", autobrowse = FALSE, 
    title = "Demo of animated lat/long for different persons",
    outdir=getwd())
于 2013-06-19T11:37:42.423 回答
1

你的问题有点含糊,但我会分享我过去是如何制作这种动画的。

  1. 创建一个函数,绘制一个时间片的所有主题位置:

    plot_time = function(dataset, time_id) {
        # make a plot with your favorite plotting package (e.g. `ggplot2`)
        # Save it as a file on disk (e.g. using `ggsave`), under a regular name, 
        # frame001.png, frame002.png, see sprintf('frame%03d', time_index)
    }
    
  2. 在每个时间片上调用此函数,例如使用lapply

    lapply(start_time_id:stop_time_id, plot_time)
    

    导致硬盘驱动器上的一组图形文件frame001调用framexxx.

  3. 使用工具将这些帧渲染成影片,例如使用ffmpeg,参见示例

这是一个通用的工作流程,已经在animation包中实现了(感谢@mdsummer 提醒我)。您可能可以利用该软件包来获取动画。

于 2013-06-19T07:06:02.367 回答