states <- geojson_read("https://rstudio.github.io/leaflet/json/us-states.geojson", what = "sp")

numOfKills <- police_killings %>%
  group_by(state) %>%
  summarize(num = n()) %>%
  mutate(name = abbr2state(state))

state_mapping <- data.frame(state = state.abb, full_name = state.name)

states@data <- left_join(states@data, numOfKills, by = "name")
states@data <- left_join(states@data, select(police_killings, state, pop), by = c("name" = "state"))

numOfKills <- merge(numOfKills, state_mapping, by = "state")

print(numOfKills)
##    state num           name      full_name
## 1     AK   2         Alaska         Alaska
## 2     AL   8        Alabama        Alabama
## 3     AR   4       Arkansas       Arkansas
## 4     AZ  25        Arizona        Arizona
## 5     CA  74     California     California
## 6     CO  12       Colorado       Colorado
## 7     CT   1    Connecticut    Connecticut
## 8     DE   2       Delaware       Delaware
## 9     FL  29        Florida        Florida
## 10    GA  16        Georgia        Georgia
## 11    HI   4         Hawaii         Hawaii
## 12    IA   2           Iowa           Iowa
## 13    ID   4          Idaho          Idaho
## 14    IL  11       Illinois       Illinois
## 15    IN   8        Indiana        Indiana
## 16    KS   6         Kansas         Kansas
## 17    KY   7       Kentucky       Kentucky
## 18    LA  11      Louisiana      Louisiana
## 19    MA   5  Massachusetts  Massachusetts
## 20    MD  10       Maryland       Maryland
## 21    ME   1          Maine          Maine
## 22    MI   9       Michigan       Michigan
## 23    MN   6      Minnesota      Minnesota
## 24    MO  10       Missouri       Missouri
## 25    MS   6    Mississippi    Mississippi
## 26    MT   2        Montana        Montana
## 27    NC  10 North Carolina North Carolina
## 28    NE   6       Nebraska       Nebraska
## 29    NH   1  New Hampshire  New Hampshire
## 30    NJ  11     New Jersey     New Jersey
## 31    NM   5     New Mexico     New Mexico
## 32    NV   3         Nevada         Nevada
## 33    NY  14       New York       New York
## 34    OH  10           Ohio           Ohio
## 35    OK  22       Oklahoma       Oklahoma
## 36    OR   8         Oregon         Oregon
## 37    PA   7   Pennsylvania   Pennsylvania
## 38    SC   9 South Carolina South Carolina
## 39    TN   6      Tennessee      Tennessee
## 40    TX  46          Texas          Texas
## 41    UT   5           Utah           Utah
## 42    VA   9       Virginia       Virginia
## 43    WA  11     Washington     Washington
## 44    WI   5      Wisconsin      Wisconsin
## 45    WV   2  West Virginia  West Virginia
## 46    WY   1        Wyoming        Wyoming
getColor <- function(num) {
  ifelse(num >= 0 & num <= 5, "lightblue",
    ifelse(num >= 6 & num <= 10, "blue",
      ifelse(num >= 11 & num <= 15, "darkblue",
             ifelse(num > 15, "purple", "grey"))
    )
  )
}

bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = numOfKills$num, bins = bins)

choropleth <- leaflet(states) %>%
  setView(-96, 37.8, 4) %>%
  addTiles(urlTemplate = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
           options = tileOptions(maxZoom = 19,
                                 attribution = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>')) 
choropleth <- choropleth %>%
  addPolygons(data = states, 
              color = ~getColor(num), 
              label = ~paste("State: ", name, "Number Of Deaths: ", num, "Population: " , pop), 
    dashArray = "10",
    highlightOptions = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE)) 
choropleth <- choropleth %>%
  addLegend(position = "bottomright",
    colors = c("lightblue", "blue", "darkblue", "purple", "grey"),
    labels = c("0-5 Deaths", "6-10 Deaths", "11-15 Deaths", "15+ Deaths", "No Data"),
    title = "Number Of Death",
    opacity = 1) 

saveWidget(choropleth, file = "interactive_map.html")

print(choropleth)