Prison Locations around the USA

September 8, 2015
By

(This article was first published on StatOfMind, and kindly contributed to R-bloggers)

I recently discovered the enigma.io resource, a repository of freely available public with the following goals (as stated on their website):

The volume of data created by governments and businesses is growing exponentially. Organizations struggle just to store it all, let alone make sense of it. Enigma helps organizations and individuals fuse, organize, and explore data to make smarter decisions.??

It is my intention to start playing around with a lot of the data they have made available, and I figured I would start easy and produce a map of prison locations around the USA (Full disclosure, this is inspired from another post Iíve seen, but I canít seem to find the original. if you have any idea what I am talking about then please send me the original so I can reference it fairly!).

A map of USA prisons


The main point of this post is to illustrate how simple it is to produce interactive leaflet maps using the leaflet and htmlwidgets package in R. in fact, it is only a single line of code to produce the map below, the rest is just processing the data into the correct format. A word of advice when using the leaflet package at the zipcode level: the library expects 5 digits zipcode, while many databases produce zipcodes that sometimes contain 4 digits. If that happens, it is necessary to pad ď0Ēs to the zipcode until the string in question has a length of five. This can be achieved using a neat little trick that is shown in line 11 of the code below!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
library(dplyr)
library(data.table)
library(leaflet)
library(htmlwidgets)

# read in prison details and zip coordinates
prisons <- fread(input="enigma-enigma.prisons.all-facilities.csv")
zip <- fread(input="zip_code_database.csv")

# pad zip code with zeros
zip[, 'zip_code' := sprintf("%05d", zip)]

# remove trailing zip codes
prisons[, 'zip_code' := unlist(lapply(strsplit(prisons$zip, split='-'), function(x) x[1]))]

# merge coordinates to prison details
df <- merge(zip[, list(zip_code, latitude, longitude)],
      prisons[, list(zip_code, type, facility_name, facility_address1, city, state)], by='zip_code')

# add popup information
df[, 'popup_info' := paste(sep = "
"
, paste0("", facility_name, ""), facility_address1, city, state)] # plot prison locations on map p <- leaflet(df) %>% addTiles() %>% addCircleMarkers(lng = ~longitude, lat = ~latitude, radius = 5, color = "red", stroke=FALSE, fillOpacity = 0.5, popup=~popup_info) saveWidget(p, file='prison_map_usa.html')

To leave a comment for the author, please follow the link and comment on their blog: StatOfMind.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)