gpt4 book ai didi

r - 在 r 中动画 ggmap

转载 作者:行者123 更新时间:2023-12-02 00:03:36 25 4
gpt4 key购买 nike

我有一个包含经纬度坐标的数据集。到目前为止,我可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com