cmelan
cmelan

Reputation: 257

How to plot and animate coordinates (latitude/longitude) data in R?

I have 10 lat and long points. With the code below, I can plot the coordinates, draw arrows in the order of their sequence and out-put a gif file that shows navigation order.

So far I was able to do this only with plot {graphics} function. because arrow {graphics} and saveGIF{animation} only seems to work with the plot function. I am wondering if its possible to this a more appropriate library such as ggmap (edit: I had mistakenly said ggplot), googleVis and etc.

library(graphics)
library(animation)

lat <- c(34.083398,34.072467,34.030237,34.597334,34.587142,34.488386,33.443484,33.946902,33.062739,32.273711,32.272611)

lon <- c(-107.907107,-106.893156,-107.971542,-105.225107,-105.13397,-103.196355,-104.52479,-103.655698,-106.0156,-107.71744,-107.713977)

coords = data.frame(lat,lon)

x <- coords$lat ; y <- coords$lon 
s <- seq(length(x)-1)  # one shorter than data


saveGIF({
  for(s in 1:length(x)){
    plot(x,y)  
    arrows(x[s], y[s], x[s+1], y[s+1], col = 1:s)
  }  
})

Upvotes: 2

Views: 1552

Answers (1)

Spacedman
Spacedman

Reputation: 94317

Yes, just remember to wrap your ggplot calls in print so they produce output. Toy example:

data=data.frame(i=1:100,x=runif(100),y=runif(100))
saveGIF({for(i in 2:100){print(ggplot(data[1:i,],aes(x=x,y=y))+geom_point())}})

Just write your code to produce each frame with ggplot2 functions and wrap in print. What did you try?

Upvotes: 2

Related Questions