Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The love of one’s country is a splendid thing. But why should love stop at the border? (Pablo Casals, Spanish cellist)
Some time ago, I discovered Enigma, an amazing open platform that unifies billions of records from thousands of government sources to make the world of public data universally accessible and useful. This is the first experiment I have done using data from Enigma. This is what I did:
- Create a free account, search and download data. Save the
csv
file in your working directory. File contains information about all prison facilities in the United States (private and state run) as recorded by the Department of Corrections in each state. Facility types, names, addresses (or lat/long coordinates) ownership names and detailed. In sum, there is information about 1.248 prison facilities. - Since most of the prisons of the file do not contain geographical coordinates, I obtain latitude and longitude using
geocode
function fromggmap
package. This step takes some time. I also remove closed facilities. Finally, I obtain a data set with complete information of 953 prison facilities. - After cleaning and filling out data, generating the map is very easy using leaflet package for R. I create a column named
popup_info
pasting name and address to be shown in the popup. Instead using defaultOpenStreetMap
basemap I use aCartoDB
one.
In my opinion, resulting map is very appealing with a minimal effort. Since I cannot embed the map here, this is a screenshot of it:
library(dplyr) library(ggmap) library(leaflet) setwd("YOUR WORKING DIRECTORY HERE") prisons = read.csv(file="enigma-enigma.prisons.all-facilities-bd6a927c4024c16d8ba9e21d52292b0f.csv", stringsAsFactors=FALSE) prisons %>% mutate(address=paste(facility_address1, city, state, zip, "EEUU", sep=", ")) %>% select(address) %>% lapply(function(x){geocode(x, output="latlon")}) %>% as.data.frame %>% cbind(prisons) -> prisons prisons %>% mutate(popup_info=paste(sep = "<br/>", paste0("<b>", facility_name, "</b>"), facility_address1, city, state, zip)) %>% mutate(lon=ifelse(is.na(longitude), address.lon, longitude), lat=ifelse(is.na(latitude), address.lat, latitude)) %>% filter(!is.na(lon) & !grepl("CLOSED", facility_name)) -> prisons leaflet(prisons) %>% addProviderTiles("CartoDB.Positron") %>% addCircleMarkers(lng = ~lon, lat = ~lat, radius = 3, color = "red", stroke=FALSE, fillOpacity = 0.5, popup = ~popup_info)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.