R Markdown and Leaflet (Atlanta Example)

R Markdown and Leaflet

June 25 2017 Saul Cruz

Objective

The objective of this post is to do a very simple exploratory analysis of crime in Atlanta, Georgia. I recently moved to Atlanta from Houston, and found a very nitty tool called Leaflet, in this post I will show a simple use case of this library.

Pre-requisites

install.packages(leaflet) library(leaflet)

Dataset

The dataset used for this exploratory analysis can be found in University of Georgia Police Department

The last update was on May 10, 2017, it contains 10.4 K records

Data Cleansing

These are the steps followed to clean the data, make it more readable and relevant. This is not feature selection since we’re not predicting yet.

Since we are only interested in crimes which take place in Atlanta, we need to restrict the data set. As stated in Kahle & Wickham’s article To determine a bounding box, we use google maps to define the top and bottom boundaries (lats and lons), then we create the map using qmap from ggmap package

atlanta<-data[-84.43743<=data$longitude & data$longitude<=-84.36238 & 33.70419<=data$latitude & data$latitude<=33.76848,]

Exploratory Analysis using Leaflet

Looks like the input data is not correct as most of the locations is the actual Georgia State Capitol.

However, this simple example shows the use of addMarkers , the use of Popups and labels, I’m using the paste function to concatenate multiple labels.

crimeLatLong<-data.frame(lat=atlanta$latitude,lng=atlanta$longitude)
crimeTitles<-atlanta$incident_type_primary
dateTitles<-atlanta$incident_datetime
incidentTypeTitles<-atlanta$parent_incident_type

crimeLatLong%>% 
  leaflet()%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions(), popup = paste("Crime:",crimeTitles, "<br>",
                                                                    "Date:",dateTitles, "<br>",
                                                                    "Type:",incidentTypeTitles))

References

Coursera Week 2 (R Markdown) Product Development Tools

Written on June 25, 2017