Philadelphia has an abundance of open data which can be found on the Open Data Philly website. I decided to take a look at the crime data from 2006 through 2014 and create a Shiny app, which you can view here, to visualize the data. The rest of this post will highlight only certain portions of the code, but the complete code for the app can be found here.
Yearly Crime Trend
My first step in exploring the data was to create a yearly crime trend plot. The goal was to create a trend plot based on two user inputs: crime type and year the crime occurred. I imported all the 2006-2014 csv files using RCurl then combined them into a single dataframe. From this point I am left with a dataframe, ds, which contains information about each incident. I narrow down this information into the number of incidents on each individual day grouped by crime type, the column labeled TEXT_GENERAL_CODE. Below is the code which uses functions from the dplyr and lubridate packages.
I originally split the data by day and crime type so that I could look at a variety of temporal trends, but for now I will focus on the monthly incident counts. Therefore to create the plot I need to filter the data for the selected crime type and year. To create the plot I will use ggplot2 as shown below. The app contains two input selectors for crime type, input$crime.type and year, input$year.
The resulting crime trend plot is shown in the animation below.
Mapping Crime with Leaflet
The second visualization is a map built using the leaflet package which is essentially a wrapper for leaflet.js. Similar to the crime trend graph, I want to show the incidents based on the user selected crime type and year. I use the filter function from dplyr to select the requested data. The leaflet() function creates a map widget. Then I add the base map tile layer to the map widget using the addProviderTiles() function. I selected a third-party base map, CartoDB.Positron, but many others options are available here. To set the level of zoom and the center coordinates of the map, the setView() function is used. The final step is to add the markers for each incident to the map. However I have a very large number of incidents to map, so I elected to use the leaflet cluster plugin which allows markers which are close to one another to be grouped together. When clicking on a cluster it zooms in to the clustered area and shows individual incidents.
The resulting map can be seen below.
I built this app as an exercise to learn how to use the leaflet package in R, but I find myself wanting to continue exploring the data. The following ideas come to mind:
Seasonal Trend Analysis: Does the rate of crime increase/decrease based on the time of year and/or location? Do these trends correlate with specific events?
Crime Data Part II: Philadelphia released additional crime data on 10/1/2015 which provides times and locations of different types of crime not provided in the datasets I originally analyzed.
Please feel free to contact me via Twitter or Github if you would like to contribute or provide feedback in regards to this project.