简体   繁体   English

R语言如何为ggmap和动画包绘制的多个图像制作动画图

[英]R language how to make an animation map for several image plotted by ggmap and animation packages

I have a .csv file of devices positions with columns "Date","time","lat","lon","radius", "position_method". 我有一个设备位置的.csv文件,其中包含“日期”,“时间”,“纬度”,“长期”,“半径”,“ position_method”列。 I want to plot the points data on Google map and make animation to reflect points changes by hour. 我想在Google地图上绘制点数据,并制作动画以按小时反映点变化。

I used the powerful animation package ( http://cran.r-project.org/web/packages/animation/animation.pdf . ) and a demo ( http://xccds1977.blogspot.fi/2012/06/ggmap.html ). 我使用了功能强大的动画包( http://cran.r-project.org/web/packages/animation/animation.pdf )和演示( http://xccds1977.blogspot.fi/2012/06/ggmap)。 html )。

The codes are as following: 代码如下:

all <- read.csv("C:/probes/2014-04-07.csv",header=T)

#convert the time format to standard hour-min-sec format
th < -strptime(all$time,"%H:%M:%OS")

#select the hour part of time
byhour <- th$h

#plot map by hours 
plotfunc <- function(x) {
  df <- subset(all,byhour <= x)

  p <- ggmap(get_googlemap (center = 'Tampere', zoom=10,maptype = 'terrain'),,extent='device')+
    geom_point(data = df,aes(x = lon, y = lat),colour = 'red',alpha=0.7)
}

#get the hours of probes
time <- sort(unique (byhour))

#generate and save the animation in HTML
saveHTML({ 
           for( i in time) print (plotfunc(i))
           ani.options(interval = 0.5)

          }, 
img.name = "probes_hour_plot", title = "Visualization of probes from Pirkanmmaa changing by time in a day",
description=c("the numbers of devices receiveing from 12am")
)

By the loop for( i in time) print(plotfunc(i) I got an animation but it overlay new points on old ones, so the points are increasing. It could not reflect the changes of distribution. Do you know how to make the animation just show the points in each hour one by one rather than any overlay and accumulation? Only what I need is just making animation combining independent images. 通过for( i in time) print(plotfunc(i)的循环for( i in time) print(plotfunc(i)我得到了一个动画,但是它在旧点上覆盖了新的点,因此点在增加。它不能反映分布的变化。您知道如何制作动画只是一个小时地显示每个小时的点数,而不是任何叠加和累积的点,仅我需要的就是使动画结合独立的图像。

The animation I got is like the gif in the demo from Kai Xiao, but it is not what I wish. 我得到的动画就像Kai Xiao演示中的gif,但这不是我想要的。

The problem isn't the animation program, it's simply the function you are using to plot your data, right now you are doing 问题不在于动画程序,它只是您用于绘制数据的函数,现在您正在执行

df <- subset(all,byhour <= x)

which selects more and more data as x increases. 随着x增加,它将选择越来越多的数据。 Try 尝试

df <- subset(all,byhours>=x-1 & byhour<= x)

instead to use more of a rolling window rather than a cumulative subset. 而是更多地使用滚动窗口而不是累积子集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM