Introduction

An interactive map of the reported crime incidents in the city of Pittsburgh, Pennsylvania, has been coded in R programming language and created with R Markdown (http://rmarkdown.rstudio.com) and Leaflet (http://leafletjs.com/).

The dataset used to create this interactive map was released by City of Pittsburgh Police and download from the following URL: https://data.wprdc.org/dataset/arrest-data. It contains only the most recent data (2016 and 2017).

R Code and Crime Map

Set working directory and load required packages.

setwd("C:/Open/Coursera/Leaflet")
library(leaflet)
library(dplyr)

Load Pittsburgh Crime Dataset.

pgh.crime.data <- read.csv("C:/Open/Coursera/Leaflet/Pgh_Crime_Data.csv")

Remove the data with missing longitudes and latitudes.

pgh.crime.data <- filter(pgh.crime.data, Longitude != 0)
pgh.crime.data <- filter(pgh.crime.data, Latitude  != 0)
pgh.crime.data <- filter(pgh.crime.data, Longitude != "")
pgh.crime.data <- filter(pgh.crime.data, Latitude  != "")

The dataset now contains 13137 recorded crime incidents.

icons <- awesomeIcons(
  icon = 'glyphicon-flag',
  iconColor = 'red')
pgh.crime.map <- leaflet(data = pgh.crime.data) %>% 
  addTiles() %>%
  addAwesomeMarkers(
    lng = ~ Longitude, 
    lat = ~ Latitude,
    clusterOptions = markerClusterOptions())
pgh.crime.map