1

I am trying to use a ggplot call inside of ddply as input to the animate package to create a time series world traffic animation. Here is my code;

library("ggmap")
library(maptools)
library(maps)
library(plyr)

......


#create map which we will then populate
mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders


plots<-dlply(b, .(b$day), function(b) ggplot(b, aes(x=lon, y=lat, color=country, 
group=country)) +   mapWorld + geom_point(data=b, aes(x=lon, y=lat, 
size=counts/100))+scale_size_identity(trans="sqrt")+guides(colour = guide_legend(override.aes 
= list(size=3))))

library(animation)
#ani.options(interval=.05)

animation::saveVideo(plots, interval=0.5, video.name="world_animation.gif")

b is a data frame containing day eg. 01/01/2014,lon eg 73, lat e.g. 52, country e.g. US, counts e.g. 25.

However I keep getting the following error:

[image2 @ 0x7fd96c817000] Could find no file with path 'Rplot%d.png' and index in the range 0-4
Rplot%d.png: No such file or directory

Any suggestions?

Thanks!

Ujjwal
  • 3,088
  • 4
  • 28
  • 36
user3466328
  • 398
  • 1
  • 11
  • 1
    It helps when you post code that is [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Include sample data so we can run the same code and get the same error. Also, it might help to include the results of `traceback()` to see where the error is really coming from. Do you have proper write permissions in your current working directory (`getwd()`)? What OS and R version are you running (show the results of `sessionInfo()`). – MrFlick Dec 22 '14 at 16:45
  • If you are on Windows, I think you need to set path to ImageMagick. But again, as MrFlick mentioned, without reproducibility there is not point in asking for suggestions. – Khashaa Dec 30 '14 at 11:25

1 Answers1

4

Using the animation, ggmap and ggplot2 packages, I could create time series animation of world cities. The animation was created using the saveHTML function in animation package, and can be viewed in a browser.

library(ggmap)
library(ggplot2)
library(animation)

#Create a data frame
countryDF<- data.frame(c("united states", "france", "india"))
colnames(countryDF) <- "countryname"
LatLon <-  c(apply(countryDF, 1, geocode))
LatLonDF <- do.call(rbind.data.frame, LatLon)
countryLatLonDF <- cbind(countryDF, LatLonDF)
countryLatLonDF[,"myDate"]<- c("02/12/13", "03/16/14", "01/10/13")
countryLatLonDF$myDate <- as.Date(countryLatLonDF$myDate , "%m/%d/%y")
countryLatLonDF["counts"] <- as.numeric(c(10,20,30))

#Sort countryLatLonDF based on dates
countryLatLonDF <- countryLatLonDF[ order(countryLatLonDF[,4]), ]

#Create animation in HTML file
saveHTML({
for (i in 1:nrow(countryLatLonDF)) 
{          

#Get the map
myMap <- ggmap(get_map(location = c(lat=0, lon=0), color="color",source="google", maptype="terrain", zoom=2))
myMap <- myMap + geom_point(data = countryLatLonDF[i,], aes(x = lon, y = lat, color = countryname, alpha = 0.5, fill = "red"), size = 5, shape = 21) + geom_text(data = countryLatLonDF[i,], aes(x = lon, y = lat, label = countryname), size = 3, vjust = 0, hjust = -0.1, color = "blue") + scale_colour_discrete(name  = "countryname")
print(myMap)
} 

}, img.name = "anim_plot", imgdir = "anim_dir", htmlfile = "anim.html", autobrowse = FALSE, title = "Country animation", verbose =FALSE, interval = 2)

graphics.off()

The HTML animation file will be saved in the working directory. You can set the working directory using setwd(). Hope this helps.