r - 在 r 中动画 ggmap

标签 r animation ggmap

我有一个包含经纬度坐标的数据集。到目前为止,我可以使用 ggmap 和 geom_path 在 map 上可视化轨道。这很好用。现在我正在尝试为轨道设置动画,轨道每 10 秒就会被路径的彩色部分填充一次。所以,这是一种轨道模拟。我已经检查了动画包,但得到的是一个空的 gif 文件。另外,我尝试使用 Sys.sleep() 函数,但它已经显示了整个轨道。 以下是坐标的示例数据集“temp”:

    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

因此,使用以下代码我可以可视化轨道。这很好用:

    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 用于我的模拟,但在我运行代码后,“ImageMagic”或“GraphicsMagic”显示输出文件,这只是白屏,没有任何动画或图像.我正在执行的代码如下:

   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)

我使用了这个 website 中的示例.

谁能给个提示?

提前致谢!

最佳答案

您必须打印 ggplot 对象。 以下是您的数据示例:

    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)

enter image description here

关于r - 在 r 中动画 ggmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19817295/

相关文章:

javascript - 查询选择器全部在 rvest 包中

r - 数据表,按组去除前导缺失值

javascript - 无法实现流畅的javascript动画

objective-c - 带回调的 CAAnimation

jquery - 翻转动画在动画过程中只渲染一半

R ggiraph with ggmap for interactive/reactive longitude and latitude points - scale or axis issue?

r - 可以结合 DT、可格式化和 Shiny 吗?

r - 将函数应用于 dplyr 的 group_by 的输出

r - 如何从ggmap中删除边距

r - 使用 ggmap 缓存 map