在R中制作动态ggmap

3

我有一个包含经纬度坐标的数据集。目前,我可以使用ggmap和geom_path在地图上可视化轨迹。这个方法运行得很好。现在我想要将轨迹动画化,每10秒钟填充路径的一部分以呈现彩色效果,这是一种轨迹模拟。我尝试了animation包,但是只得到了一个空白的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”包含每个点的时间和日期等数据。因此,我尝试使用“animation”包来获取我的模拟的.*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)

我使用了来自这个网站的示例。

有人能给一个提示吗?

提前致谢!


请提供一个可重现的示例 - https://dev59.com/eG025IYBdhLWcg3whGSx。你是否查看了 https://github.com/hadley/ggplot2/wiki/Using-ggplot2-animations-to-demonstrate-several-parallel-numerical-experiments-in-a-single-layout? - EDi
感谢您的回复!我已经编辑了我的问题。 - ArmMiner
1个回答

3
你需要打印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


谢谢!我刚试了这段代码,但输出的是一个带有坐标但没有路径的x-y系统。 - ArmMiner
执行上面的代码是什么时候?好吧,我得到了一个不错的动画...当然你必须根据自己的需求进行调整... - EDi
你在哪里看到输出?在ImageMagic还是GraphicsMagic中?能否在地图上查看? - ArmMiner
当您运行代码时,您是否看到以下内容? geom_path:每个组仅包含一个观察。您需要调整组美学吗? 正在执行: "C:\Program Files\ImageMagick-6.8.7-Q16\convert.exe" -loop 0 -delay 10 C:/Users/Armen/AppData/Local/Temp/RtmpUzXYVP/Rplot1.png ... C:/Users/Armen/AppData/Local/Temp/RtmpUzXYVP/Rplot16.png "C:\Users\Armen\AppData\Local\Temp\RtmpUzXYVP/animation.gif" 输出位于: C:\Users\Armen\AppData\Local\Temp\RtmpUzXYVP/animation.gif - ArmMiner
你找不到已创建的GIF在你的电脑上吗?(C:\Users\Armen\AppData\Local\Temp\RtmpUzXYVP/animation.gif)?在我的电脑上,GIF会自动打开查看器 - 但文件位于所提到的位置。不知道问题出在哪里... - EDi
我可以找到并查看它,但它不是动画。它只是最终的图像! - ArmMiner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接