The Fairfax County Police Department in Fairfax, Virginia has an easy-to-use, unauthenticated API for pulling weekly crime stats from their site. I used the httr package to do this.
library(httr)
A simple GET from that URL produces the data necessary
a<-GET('https://www.fairfaxcounty.gov/apps/pfsu/api/file/crimereportsfromsp')
df<-content(a)
## No encoding supplied: defaulting to UTF-8.
## Parsed with column specification:
## cols(
## `1` = col_double(),
## `FRAUD-26A-1` = col_character(),
## `FRAUD - FALSE PRETENSES/SWINDLE/CONFIDENCE GAME` = col_character(),
## `2020-01-24` = col_date(format = ""),
## `0736` = col_character(),
## `4100 GALESBURY LN` = col_character(),
## CHANTILLY = col_character(),
## VA = col_character(),
## `20151` = col_double()
## )
colnames(df)<-c('repnum','code','popup','date','time','street_address','city','state','zip')
I used ggmap’s geocode function to turn the addresses into latLongs
library(ggmap)
df$address<-paste(df$street_address,df$city, df$state)
df$lat<-rep(0,nrow(df))
df$lng<-rep(0,nrow(df))
df$geoAddress<-rep('',nrow(df))
for (i in 1:nrow(df)){
result <- suppressMessages(geocode(df$address[i], output = "latlona", source = "google"))
df$lng[i] <- as.numeric(result[1])
df$lat[i] <- as.numeric(result[2])
df$geoAddress[i] <- as.character(result[3])
}
You can zoom into the resulting Leaflet map, and click on the individual markers to see what the description of the police call-out was.
## Assuming "lng" and "lat" are longitude and latitude, respectively
Here is the code that produced the above chart:
library(dplyr)
library(leaflet)
dates<-as.factor(df$date)
col=rainbow(7)[as.integer(dates)]
df$col<-col
df[,c(11,12,14)] %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(clusterOptions = markerClusterOptions(), popup=df$popup, col=df$col) %>%
addLegend(labels = levels(dates), colors = rainbow(7), title = paste(range(df$date)))