ggAnimating London’s Heartbeat

Just the other day, #gganimate was published on CRAN. Although a considerable number of R users have given it a go in the last 24 hours in creating funny or serious animations, I thought I’d explain here step by step how I went about doing one of my animations. Compared to the github version, I found the CRAN version including the updated instructions very helpful in getting a handle of everything.

So, here it is!

The animation plots the incidence of violence and sexual offences in London during 2017 by month. I do not want to read too much into what is purely a programming exercise, but the level of offences appears strikingly lower in August/September compared to the other months. This may or may not be right, I leave it to scholars of crime economics to determine its verity. Because of the London’s map shape and the inherent inwards-outwards distribution of the points, I call this animation London’s Heartbeat.

So, here’s the most relevant bits. I use data from the Metropolitan Police Service for 2017. I call it the “crime” data. I also needed London’s shapefile which I obtained relatively easy. The crime data allows me to pick any offence among: Violence and sexual offences, Anti-social behaviour, Criminal damage and arson, Drugs, Public order, Vehicle crime, Burglary, Other theft, Shoplifting, Robbery, Theft from the person, Other crime, Bicycle theft, Possession of weapons, from which I simply chose the first: Violence and sexual offences.

Let’s start with making London’s map in R

london <- ggplot(shape.final) +
geom_polygon(aes(x = long, y = lat, group = group), fill="white", colour="grey60")

Adding the crime data (subselected only for violence and sexual offences)

london + geom_point(data = crime2, aes(x = long, y = lat, colour = crime_violent)) 

which gets us almost where we want

Finally, adding the animation by month:

london + geom_point(data = crime2, aes(x = long, y = lat, colour = crime_violent), shape=4, colour="goldenrod1")  + 
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
labs(caption = "Credits: Raluca L. Pahontu, Jan 2019") +
labs(title = 'Map of London Violence and Sexual Offences; 2017, Month: {frame_time}') +
transition_time(Month)

And this is it! Stay tuned for more animations!

Leave a Reply

Your email address will not be published. Required fields are marked *