Site icon R-bloggers

Animated map of 2012 US election campaigning, with R and ffmpeg

[This article was first published on Civil Statistician » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(Video link here, in case the embedded player doesn’t work for you.)

Idea: see if I can mimic the idea behind Ben Schmidt’s lovely video of ocean shipping routes, and apply it to another dataset. But which?
“Hmm… what’s another interesting dataset about some competitors traveling around a mostly-fixed area at the same time?… Hey friends, stop  giving me election news, I need to think of an idea… Oh.”

Get data: scraped from the Washington Post’s interactive graphic of 2012 Presidential Campaign Stops. (Initially I asked the Post for the data, but they are unable to share it, under their agreement with the data provider. However, it turns out you can find it by poking around inspecting the website elements…)
However, this data doesn’t tell me whether the candidates went home after all the day’s events or kept traveling. To keep it simple, I’m only plotting the travel between event locations — but in reality, Obama should have many more zips to and from DC, and likewise for Romney and Boston.

Clean and prepare the data: urgh.

Geocoding the data: I basically used the geocoding example code from r-chart.com, and looped through all the event locations, except that

Plot the data: based on the code from Ben Schmidt’s comment here. Instead of using a for-loop to generate all the images, I’m using saveHTML() in the animation package. This lets me see what the animation will look like without having to go through ffmpeg (see below), so I can test out changes on just the first 100 frames and preview the revised animation.

Animate the plots: using ffmpeg, the same free tool Ben Schmidt used. (Here’s the Windows download in case you have trouble finding it. It’s a great tool, but I wish the site layout and documentation were more newbie-friendly.)
I put a copy of ffmpeg.exe in the directory where saveHTML() put all the images. And I also put a copy of the mp3 there. Then at the command line, I run (all on one line):
ffmpeg -y -f image2 -r 40500/1001 -i "travelplot%d.png" -i BumbleBee.mp3 -sameq TravelMaps.mpg
Tweak the numerator in 40500/1001 to  change the frame rate (hence speed and length) of the video. Remove “-i BumbleBee.mp3” if you have no music.

Upload to Vimeo: Vimeo.com has nice video upload and editing features, although be prepared to wait a while — after the upload is complete, you have to wait for them to convert it to their own format before it’s actually posted & viewable.

Ideas for improvement:

To leave a comment for the author, please follow the link and comment on their blog: Civil Statistician » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.