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.


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

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.



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

for(i in 1:length(times)){
    t <- times[i]
    title <- paste('tsplot', i, ".png", sep="")
    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")

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.


A Simple Tutorial of the ggmap package in R

The R package ggmap allows ggplot2 capabilities with google maps. I have recently used it for a few projects, and wanted to save a few quick sample plots so I can easily return to creating similar plots. Here are a few examples of ggmap plots.

The first example uses the ‘crime’ dataset that comes in the ggmap package. This dataset shows crimes in the Houston area.



houston &lt;- get_map(location = 'Houston', zoom = 10, maptype = "roadmap")

ggmap(houston) +
geom_point(data = crime[crime$offense == "murder",],
mapping = aes(x = lon, y = lat),
color="hotpink", shape=3, size=3) +
ggtitle("ggmap example using\nthe crime R dataset") +
theme(plot.title = element_text(hjust = 0.5))

This second plot is an example using the satellite plot type. This plot shows eathquakes near Fiji. The data is from the ‘quakes’ dataset in the R package ‘datasets’.



fiji <- get_map(location = c(mean(quakes$long), mean(quakes$lat)), zoom = 5,
maptype = "satellite")

ggmap(fiji) +
geom_point(data = quakes, mapping = aes(x = long, y = lat, color = depth, size=mag)) +
scale_colour_gradient(low = "hotpink",high = "yellow") +
ggtitle("ggmap example using\nthe Fiji earthquakes R dataset ") +
theme(plot.title = element_text(hjust = 0.5))