Question

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

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")

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)

I've used the example from this website.

Could anybody give a hint?

Thanks in advance!

Was it helpful?

Solution

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)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top