Data on motor vehicle collisions in New York City has been made accesible by NYC Open Data

library(ggplot2);library(ggmap);library(dplyr);
library(reshape2);library(data.table);library(gridExtra);

Munging the data

collisions<-read.csv('~/Desktop/NYPD_Motor_Vehicle_Collisions.csv')
collisions$DATE<-as.Date(collisions$DATE,"%m/%d/%Y")

columns<-which(colnames(collisions)%in%c("DATE","LATITUDE","LONGITUDE",
                                        "NUMBER.OF.PEDESTRIANS.INJURED","NUMBER.OF.PEDESTRIANS.KILLED",
                                        "NUMBER.OF.CYCLIST.INJURED","NUMBER.OF.CYCLIST.KILLED",
                                        "NUMBER.OF.MOTORIST.INJURED","NUMBER.OF.MOTORIST.KILLED"))

collisions<-collisions[,columns] 

colnames(collisions)<-c("Date","lat","lon",
                        "Pedesterian Injured","Pedesterian Killed",
                        "Cyclist Injured","Cyclist Killed",
                        "Motorist Injured","Motorist Killed")
head(collisions)
##         Date   lat    lon Pedesterian Injured Pedesterian Killed
## 1 2014-12-16    NA     NA                   0                  0
## 2 2014-12-16 40.74 -73.98                   0                  0
## 3 2014-12-16 40.74 -74.01                   0                  0
## 4 2014-12-16    NA     NA                   0                  0
## 5 2014-12-16 40.71 -73.80                   0                  0
## 6 2014-12-16 40.65 -74.01                   0                  0
##   Cyclist Injured Cyclist Killed Motorist Injured Motorist Killed
## 1               0              0                0               0
## 2               0              0                0               0
## 3               0              0                0               0
## 4               0              0                0               0
## 5               0              0                1               0
## 6               0              0                0               0
melt.collisions<-melt(collisions,id.vars=c('Date','lat','lon'))

melt.collisions$party<-""
melt.collisions$party[grep('Pedesterian',melt.collisions$variable)]<-"Pedestrian"
melt.collisions$party[grep('Cyclist',melt.collisions$variable)]<-"Cyclist"
melt.collisions$party[grep('Motorist',melt.collisions$variable)]<-"Motorist"
melt.collisions$outcome<-""
melt.collisions$outcome[grep('Injured',melt.collisions$variable)]<-"Injured"
melt.collisions$outcome[grep('Killed',melt.collisions$variable)]<-"Killed"

head(melt.collisions)
##         Date   lat    lon            variable value      party outcome
## 1 2014-12-16    NA     NA Pedesterian Injured     0 Pedestrian Injured
## 2 2014-12-16 40.74 -73.98 Pedesterian Injured     0 Pedestrian Injured
## 3 2014-12-16 40.74 -74.01 Pedesterian Injured     0 Pedestrian Injured
## 4 2014-12-16    NA     NA Pedesterian Injured     0 Pedestrian Injured
## 5 2014-12-16 40.71 -73.80 Pedesterian Injured     0 Pedestrian Injured
## 6 2014-12-16 40.65 -74.01 Pedesterian Injured     0 Pedestrian Injured
melt.collisions<-filter(melt.collisions,value>=1)
melt.collisions<-melt.collisions[complete.cases(melt.collisions),]

Black and White Maps as ‘Base Layer’ of Visualization

nycmap_bw1 <- qmap("DeKalb Ave and Myrtle Ave, new york", zoom = 12, color = "bw", legend = "topleft")
grid.arrange(nycmap_bw1)