r中动画ggmap [英] Animating ggmap in r

查看:426
本文介绍了r中动画ggmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期和纬度坐标的数据集。直到现在,我可以用可视化和ggmap在geom_path地图上的轨道。这工作得很好。现在,我试图动画轨道的话,这条赛道将每隔10秒与路径的彩色部分填补。那么,这是怎样的一个轨迹的模拟。我已经签出动画包,但我得到一个空的gif文件。此外,我试着用Sys.sleep()函数,但它已经具备了全程跟踪。
下面是一个样本数据集'临时'的坐标:

I have a data set with long and lat coordinates. Till now I can visualize the track on the map using ggmap and geom_path. This works fine. Now I am trying to animate the track so, that the track would be filled every 10 seconds with a colored part of the path. So, this is kind of simulation of a track. I've checked out the animation package, but I am getting an empty gif file. Also, I tried with Sys.sleep() function, but it shows already the whole track. The following is a sample dataset 'temp' for coordinates:

    10.16530167 54.32216667
    10.16530167 54.32216667
    10.16529833 54.32216833
    10.165295   54.32217
    10.16529333 54.32217167
    10.16529167 54.32217167
    10.16529167 54.32217167
    10.16529333 54.32217167
    10.165295   54.32217
    10.165295   54.32217
    10.165295   54.32217167
    10.16529333 54.32217
    10.16528833 54.32217
    10.16527833 54.32216667
    10.16527167 54.32216
    10.165265   54.32215667

因此​​,用下面的code,我可以可视化的轨道。这工作得很好:

So, with the following code I can visualize the track. This works fine:

    require(ggmap) 
    require(mapproj)
    ggmap(m)+geom_path(data=track_df, aes(x=temp$GPS_x,                  
    y=temp$GPS_y),colour="red",size=1,lineend="round")

的'track_df'包含这样的数据,作为每一个点的时间和日期。所以,我试图用'动画'包获得。* GIF我的模拟,但在我运行code中的'ImageMagic'或'GraphicsMagic'显示输出文件,这只是白色屏幕,没有任何动画或图像。我正在执行code是以下内容:

The 'track_df' contains such data as the time and date for every point. So, I tried to use 'animation' package for getting the .*gif for my simulation, but after I run the code the 'ImageMagic' or 'GraphicsMagic' show the output file, which is just white screen, no any animation or image. The code that I am executing is the following:

   require(animation)
   npoints<- length(temp$GPS_x)
   simulate <- function(){
   for(i in 1:npoints){
   ggmap(m)+geom_path(data=ricky_df, aes(x=temp$GPS_x[i],  
   y=temp$GPS_y[i]),colour="red",size=1,lineend="round")
   }
   }
   oopt = ani.options(interval = 1, nmax = npoints)  
   saveMovie(simulate(),interval = 0.1, width = 580, height = 400)

我已经从这个网站使用的示例。

任何人可以给出一个提示吗?

Could anybody give a hint?

在此先感谢!

推荐答案

您必须打印ggplot对象。
这里是你的数据为例:

You have to print the ggplot-object. Here is an example with your data:

    pts <- read.table(text = '10.16530167 54.32216667
    10.16530167 54.32216667
    10.16529833 54.32216833
    10.165295   54.32217
    10.16529333 54.32217167
    10.16529167 54.32217167
    10.16529167 54.32217167
    10.16529333 54.32217167
    10.165295   54.32217
    10.165295   54.32217
    10.165295   54.32217167
    10.16529333 54.32217
    10.16528833 54.32217
    10.16527833 54.32216667
    10.16527167 54.32216
    10.165265   54.32215667')
names(pts) <- c('x', 'y')


require(ggplot2)
require(animation)

npoints<- nrow(pts)

plotfoo <- function(){
  for(i in 1:npoints){
    take_df <- pts[1:i, ]
    p <- ggplot() +
      geom_path(data = take_df, aes(x = x, y = y)) +
      xlim(c(min(pts$x), max(pts$x))) +
      ylim(c(min(pts$y), max(pts$y)))
    print(p)
  }
}

oopt = ani.options(interval = 1, nmax = npoints)  
saveMovie(plotfoo(),interval = 0.1, width = 580, height = 400)

这篇关于r中动画ggmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆