Tutorials

Easy Animation in R

I recently animated some figures using ggplot and ImageMagick. ImageMagick is an application independent of R. A set og ggplots are created in R, and ImageMagick is used in the command line to merge the set of plots into a gif.

To get started, download https://www.imagemagick.org/script/binary-releases.php following instructions for your operating system. I work on Windows, so this tutorial will use that.

mapcrimes

library(ggmap)
#subset data to just one day for an example
crime <- crime[crime$time<'2010-01-05 0:00:00', ]
#create list of times used to save unique plots
times <- unique(crime$time)
#get map from ggmap
houston <- get_map(location = 'Houston', zoom = 10,
maptype = "roadmap",
color = "bw")

#loop through all times, and save ggplots as jpeg
for(i in 1:length(times)){
    t <- times[i]
    m <- ggmap(houston) + geom_point(data = crime[crime$time == t,],
                                     mapping = aes(x = lon, y = lat, color=offense),
                                     size=5) +
    ggtitle(paste("Houston crime\n"), t) +
    theme(legend.position="bottom") +
    scale_colour_discrete(drop = FALSE)
    title <- paste("map",i,".jpg",sep="")
    jpeg(title)
    print(m)
    dev.off()
}

Once all the jpeg plots are saved to your directory, open an command prompt and navigate to the folder where those plots are saved. Then run the following command in the command prompt. This will use all the individual jpeg files to create one animated gif.

magick *.jpg mapcrimes.gif

Here is another little example.

tscrimes

library(dplyr)
library(ggmap)

crime$date <- as.Date(crime$time)
crime <- crime[crime$date<'2010-03-01', ]
crime <- crime %>%
         group_by(date) %>%
         summarize(total=n())
times <- unique(crime$date)

for(i in 1:length(times)){
    t <- times[i]
    title <- paste('tsplot', i, ".png", sep="")
    png(title)
    plot(crime[crime$date<=t,], col="blue", main="Crimes in Houston Jan-Mar 2010",
    xlim=c(min(times), max(times)),
    ylim=c(200, 450))
    lines(crime[crime$date<=t,], lwd=2, col="blue")
    dev.off()
}

And create the gif in the command line using
magick *.png tscrimes.gif

P.S. the R package animation will produce the same graphs without using command line.

Advertisements

2 thoughts on “Easy Animation in R

  1. Nice post. I’ve not tried it but googleVis package by Markus Gesmann provides access to a number of google applications including motion plots like those made famous by Hans Rosling. Among the google interfaces made available – essentially googleVis writes HTML code that’s passed to the google API – are maps. It may be that you can create the same visualisation but would have a play and rewind button. Personally I like this sort of video. I also like to build a heat map overlay to get a send of whether the same place keeps getting impacted by the same type of event. This is something I wrote about in my last blogpost but am thinking that a panel plot of heatmaps (ie each one representing events over a specific period) would also capture the time series element in one go.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s