Chapter 20 Map of the Reporting Districts with their Crimes

We investigate the Crimes associated with the Reporting Districts.

RDGeoLocationFull = LACrime %>% 
  group_by(ReportingDistrict) %>%
  summarise(RDlat = median(latitude,na.rm = TRUE)
            ,RDlon = median(longitude,na.rm = TRUE)
            ,CountIncidents = n())

RD = readOGR(dsn = "input/LAPD_Reporting_Districts.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "input/LAPD_Reporting_Districts.shp", layer: "LAPD_Reporting_Districts"
## with 1135 features
## It has 7 fields
## Integer64 fields read as strings:  OBJECTID REPDIST PREC
RD@data$REPDIST = as.numeric(RD@data$REPDIST )
RDGeoLocationFull$ReportingDistrict = as.numeric(RDGeoLocationFull$ReportingDistrict)

RD@data = left_join(RD@data, RDGeoLocationFull, by = c("REPDIST" = "ReportingDistrict"))

bins = c(0,1000,2000, 4000, 5000, 6000, 7000,8000, 9000)

pal = colorBin("YlOrRd", domain = RD@data$CountIncidents, bins = bins)

labels = sprintf(
  "<strong>%s</strong><br/>%g",
  RD@data$REPDIST, RD@data$CountIncidents
) %>% lapply(htmltools::HTML)

center_lon = median(RD@data$RDlon,na.rm = TRUE)
center_lat = median(RD@data$RDlat,na.rm = TRUE)

leaflet(data = RD) %>%  setView(lng=center_lon, lat=center_lat, zoom=12) %>%
  addPolygons(
    fillColor = ~pal(CountIncidents),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")) %>%
  addLegend(pal = pal, values = ~CountIncidents, opacity = 0.7, title = "Reporting Districts and Crimes",
            position = "bottomleft")