Tuesday 2 June 2015

Animated map of police shootings


I wrote a simple R script to create an animated map of police shootings this year.

R packages used: RgoogleMaps to get latitude and longitude, lubridate for handling dates, anim.plots to create the animation, and maps for the US map.


Data from the Guardian story.

library(RgoogleMaps)
library(anim.plots)
library(lubridate)
library(maps)
dfr <- read.csv("~/Downloads/thecounted-data/the-counted.csv")
latlon <- sapply(paste(dfr$address, dfr$city, dfr$state,  sep = ","), getGeoCode)
dfr$lat <- latlon[1,]
dfr$lon <- latlon[2,]

dfr$race2 <- as.character(dfr$raceethnicity)
dfr$race2[! dfr$race2 %in% c("Black", "White", "Hispanic/Latino")] <- "Other"
sym.cols <- AddAlpha(c("brown4", "orange", "grey", "pink3"), .8)
dfr$racecol <- sym.cols[factor(dfr$race2)]
dfr$time <- dmy(paste(dfr$day, dfr$month, dfr$year))

map("state", ylim = c(20, 49))
legend(-125, 25, col=sym.cols, legend = c("Black", "Hispanic", "Other", "White"),
      pch = 19, horiz = TRUE, bty="n")
legend(-125, 23, pch = c(2, 1), legend = c("Armed", "Unarmed"), horiz = TRUE, bty = "n")
anim.points(dfr$lon, dfr$lat, times = dfr$time, col = dfr$racecol, pch = ifelse(dfr$armed=="Yes", 17, 19))

Result:

No comments:

Post a Comment