library("leaflet")
The NYC Economic Development Corporation, NYC Department of Sanitation, and Office of the Mayor are working on a initiative to keep the NYC Graffiti-Free. This map will show locations and resolutions of reported graffiti incidents within NYC.
The data for this initiative can be found on the data.newyorkcity.us website (https://data.cityofnewyork.us/City-Government/DSNY-Graffiti-Tracking-Map-/v9sd-nunw).
graffiti<-read.csv("C:/Users/selen/Desktop/R Folder/DSNYGraffitiTracking.csv")
str(graffiti)
## 'data.frame': 286 obs. of 24 variables:
## $ INCIDENT_ADDRESS : Factor w/ 286 levels "1 BRIGHTON 4 ROAD",..: 93 164 203 123 23 196 280 176 252 138 ...
## $ BOROUGH : Factor w/ 5 levels "BRONX","BROOKLYN",..: 2 2 2 4 1 4 2 2 3 4 ...
## $ COMMUNITY_BOARD : Factor w/ 52 levels "01 BRONX","01 BROOKLYN",..: 2 47 9 3 4 3 2 18 10 3 ...
## $ POLICE_PRECINCT : Factor w/ 63 levels "Precinct 1","Precinct 10",..: 63 47 56 14 27 14 63 51 35 14 ...
## $ CITY_COUNCIL_DISTRICT : int NA 40 36 22 17 22 NA 42 1 22 ...
## $ BBL : num NA 3.05e+09 3.02e+09 4.01e+09 2.03e+09 ...
## $ CREATED_DATE : Factor w/ 11 levels "03/05/2018","03/06/2018",..: 5 9 3 10 10 7 3 8 1 8 ...
## $ STATUS : Factor w/ 1 level "Open": 1 1 1 1 1 1 1 1 1 1 ...
## $ RESOLUTION_ACTION : Factor w/ 5 levels "Graffiti Reported",..: 3 1 2 1 5 5 3 5 5 2 ...
## $ CLOSED_DATE : logi NA NA NA NA NA NA ...
## $ X_COORDINATE : int NA 994171 1001445 1005184 1015375 1011385 NA 1013874 985181 1007977 ...
## $ Y_COORDINATE : int NA 176865 189269 221326 239950 219823 NA 180403 198229 216900 ...
## $ LATITUDE : num NA 40.7 40.7 40.8 40.8 ...
## $ LONGITUDE : num NA -74 -73.9 -73.9 -73.9 ...
## $ ZIP_CODE : int NA 11226 11221 11102 10459 11105 NA 11207 10038 11103 ...
## $ CENSUS_TRACT : num NA 50804 277 91 119 ...
## $ BIN : int NA 3394295 3045053 4020011 2006272 4538527 NA 3085551 1083359 4011358 ...
## $ NTA : Factor w/ 92 levels "","Annadale-Huguenot-Prince's Bay-Eltingville",..: 1 38 80 66 48 79 1 32 22 3 ...
## $ LOCATION : Factor w/ 276 levels "","(40.529256, -74.161656)",..: 1 51 104 246 254 239 1 54 138 228 ...
## $ Zip.Codes : int NA 13510 18181 16859 10937 16862 NA 17214 13096 16860 ...
## $ Community.Districts : int NA 60 69 39 8 39 NA 45 70 39 ...
## $ Borough.Boundaries : int NA 2 2 3 5 3 NA 2 4 3 ...
## $ City.Council.Districts: int NA 11 49 4 43 4 NA 25 32 4 ...
## $ Police.Precincts : int NA 43 52 72 24 72 NA 47 2 72 ...
This dataset includes 286 observations and 24 attributes. Since some of the columns are not tidy, I will be cleaning the data first. If there are any observations without coordinates, I will remove these observations as they will not be insightful to making my Leaflet map.
library(data.table)
colnames(graffiti)<-tolower(colnames(graffiti))
colnames(graffiti)<-gsub(" ","", colnames(graffiti))
colnames(graffiti)<-gsub("_", "", colnames(graffiti))
colnames(graffiti)<-gsub("\\.", "", colnames(graffiti))
eliminate<-graffiti[!is.na(graffiti$latitude)&!is.na(graffiti$longitude),]
str(graffiti)
## 'data.frame': 286 obs. of 24 variables:
## $ incidentaddress : Factor w/ 286 levels "1 BRIGHTON 4 ROAD",..: 93 164 203 123 23 196 280 176 252 138 ...
## $ borough : Factor w/ 5 levels "BRONX","BROOKLYN",..: 2 2 2 4 1 4 2 2 3 4 ...
## $ communityboard : Factor w/ 52 levels "01 BRONX","01 BROOKLYN",..: 2 47 9 3 4 3 2 18 10 3 ...
## $ policeprecinct : Factor w/ 63 levels "Precinct 1","Precinct 10",..: 63 47 56 14 27 14 63 51 35 14 ...
## $ citycouncildistrict : int NA 40 36 22 17 22 NA 42 1 22 ...
## $ bbl : num NA 3.05e+09 3.02e+09 4.01e+09 2.03e+09 ...
## $ createddate : Factor w/ 11 levels "03/05/2018","03/06/2018",..: 5 9 3 10 10 7 3 8 1 8 ...
## $ status : Factor w/ 1 level "Open": 1 1 1 1 1 1 1 1 1 1 ...
## $ resolutionaction : Factor w/ 5 levels "Graffiti Reported",..: 3 1 2 1 5 5 3 5 5 2 ...
## $ closeddate : logi NA NA NA NA NA NA ...
## $ xcoordinate : int NA 994171 1001445 1005184 1015375 1011385 NA 1013874 985181 1007977 ...
## $ ycoordinate : int NA 176865 189269 221326 239950 219823 NA 180403 198229 216900 ...
## $ latitude : num NA 40.7 40.7 40.8 40.8 ...
## $ longitude : num NA -74 -73.9 -73.9 -73.9 ...
## $ zipcode : int NA 11226 11221 11102 10459 11105 NA 11207 10038 11103 ...
## $ censustract : num NA 50804 277 91 119 ...
## $ bin : int NA 3394295 3045053 4020011 2006272 4538527 NA 3085551 1083359 4011358 ...
## $ nta : Factor w/ 92 levels "","Annadale-Huguenot-Prince's Bay-Eltingville",..: 1 38 80 66 48 79 1 32 22 3 ...
## $ location : Factor w/ 276 levels "","(40.529256, -74.161656)",..: 1 51 104 246 254 239 1 54 138 228 ...
## $ zipcodes : int NA 13510 18181 16859 10937 16862 NA 17214 13096 16860 ...
## $ communitydistricts : int NA 60 69 39 8 39 NA 45 70 39 ...
## $ boroughboundaries : int NA 2 2 3 5 3 NA 2 4 3 ...
## $ citycouncildistricts: int NA 11 49 4 43 4 NA 25 32 4 ...
## $ policeprecincts : int NA 43 52 72 24 72 NA 47 2 72 ...
For this Leaflet map, I used some of the basic techniques taught in class to map locations of grafitti in New York City. Aside from the basic Leaflet techniques learned in class, I used this R Studio Github (https://rstudio.github.io/leaflet/) to add different colored basemap layers and multiple-lined pop-ups. When clicking on the clusters, you will see Rich Uncle Pennybags (Main Character for Monopoly) in the locations in which there have been graffiti incidents. When you click on Rich Uncle Pennybags, you will receive information about each graffiti incident. The information contained in each pop-up will include: the incident address, borough, status of the incident, and resolution action.
I hope you enjoy! I had tons of fun making this Leaflet Map!
data(graffiti)
monopolyicon<-makeIcon(
iconUrl = "https://png2.kisspng.com/sh/a3143b3858c90a2f4c47d9e21955b8db/L0KzQYm3VsMxN5t7jJH0aYP2gLBuTf16NZ50htH5b3z8PcPwgBgufZ9ohNc2cHXxfsrpgfd0NZN0eeRtLXfkfba0VfI0P5RqSqkAZnHoR4m1UsEzPmM6UKI6NUOzQ4m6WcEyO2o2UZD5bne=/kisspng-my-monopoly-rich-uncle-pennybags-board-game-5b37ce275fae78.2126258015303839113919.png", iconWidth = 40, iconHeight = 50
)
markers<-data.frame(lat=graffiti$latitude, lng=graffiti$longitude)
markers %>%
leaflet(data=graffiti) %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
addProviderTiles(providers$Stamen.TonerLines,
options = providerTileOptions(opacity = 0.35)) %>%
addProviderTiles(providers$Stamen.TonerLabels) %>%
addMarkers(icon = monopolyicon,
clusterOptions=markerClusterOptions(), popup = paste("Incident Address:", graffiti$incidentaddress, "<br>", "Borough:", graffiti$borough, "<br>", "Status:", graffiti$status, "<br>", "Resolution Action:", graffiti$resolutionaction))
## Assuming "longitude" and "latitude" are longitude and latitude, respectively