Philadelphia Crime Density Maps Anupama Rajaram

Data Visulization using Philadelphia Crime Data from Kaggle



First we load all the required libraries (code hidden).

Read the Philadelphia crime dataset.

phildata <- fread("crime.csv")

## Read 10.2% of 2152318 rows Read 24.6% of 2152318 rows Read 39.0% of 2152318 rows Read 45.5% of 2152318 rows Read 60.4% of 2152318 rows Read 67.4% of 2152318 rows Read 81.8% of 2152318 rows Read 91.1% of 2152318 rows Read 2152318 rows and 14 (of 14) columns from 0.278 GB file in 00:00:11

Create a contour map for Philadelphia using the ggmap functions

phil = c(lon = -75.19, lat = 39.98) philmap = get_map(location = phil, zoom = 12, color = "bw")

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=39.98,-75.19&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false

We write a function to generate contour map filtered for a specific crime category. (br />Colors indiacte whether crime is high or low.

red= high_count, green=low_count

map_crime <- function(crime_data, crime_category_name) { titlestr <- paste("Heatmap for crimes related to", crime_category_name ) filterdf <- filter(crime_data, Text_General_Code %in% crime_category_name) plotimg <- ggmap(philmap, extent = "device") + geom_density2d(data = filterdf, aes(x = Lon, y = Lat), size = 0.3) + stat_density2d(data = filterdf, aes(x = Lon, y = Lat, fill = ..level.., alpha = ..level..) , size = 0.01, bins = 16, geom = 'polygon') + scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0.00, 0.25), guide = FALSE) + ggtitle(titlestr) theme(legend.position = "none", axis.title = element_blank(), text = element_text(size = 12)) return(plotimg) }

Call function to view crime-density map for Thefts map_crime(phildata, c('Thefts')) ## Warning: Removed 46423 rows containing non-finite values (stat_density2d). ## Warning: Removed 46423 rows containing non-finite values (stat_density2d).

Call function to view crime-density map for Thefts map_crime(phildata, c('Fraud')) ## Warning: Removed 24229 rows containing non-finite values (stat_density2d). ## Warning: Removed 24229 rows containing non-finite values (stat_density2d).

Call function to view crime-density map for Aggravated assault map_crime(phildata, c('Aggravated Assault No Firearm')) ## Warning: Removed 10189 rows containing non-finite values (stat_density2d). ## Warning: Removed 10189 rows containing non-finite values (stat_density2d).

Call function to view crime-density map for Narcotics map_crime(phildata, c('Narcotic / Drug Law Violations')) ## Warning: Removed 10690 rows containing non-finite values (stat_density2d). ## Warning: Removed 10690 rows containing non-finite values (stat_density2d).