2013-06-19 29 views
4

我有一组数据,其中包含800个主题的约150,000个观察值。每个观察结果都有:主题ID,纬度,经度以及主题在这些坐标上的时间。数据涵盖了24小时的时间。如何创建地理空间/时间数据的动画

如果我一次绘制所有的数据,我只是得到一个blob。任何人都可以给我一些关于如何使这些数据动画的技巧,以便我可以观察作为时间函数的主题路径?

我读过时空小插曲,但我不完全确定它会做我想做的。在这一点上,我花了很多时间在Google上搜索,但并没有真正满足我的需求。

任何提示和指针非常感谢!

+2

你有没有看动画包? – alexwhan

回答

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) 
    

    导致所谓frame001framexxx硬盘驱动器上的一组图形文件。

  3. 使用工具将这些帧渲染为影片,例如,使用ffmpeg,见for example

这是一个通用的工作流程,已在animation包被已经实现了(谢谢你提醒我@mdsummer)。你可以利用这个包来获得你的动画。

+3

绝对使用动画包而不是您的代码来生成帧:) – mdsumner

3

这里我第一次使用animation包。这比我预想的更容易,特别是saveHTML真的很棒。在这里我的方案(即使我认为我的R-代码会更清楚:)

  1. 我产生一些数据
  2. 我绘制了所有的人作为背景图的基本情节。
  3. 我重塑数据以获得宽格式的方式,我可以在每个人的现在和下一个位置之间绘制一个箭头。
  4. 我循环了几个小时,产生许多情节。我把llop放在功能强大的saveHTML函数中。
  5. 你得到一个很好的动画html文件。我在这里展示一个中间情节。

enter image description here

这里我的代码:

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())