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")

calculateDeathRate <- function(num, pop) {
  death_rate = (num / pop) * 10000
  return(round(death_rate))
}

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(death_rate) {
  ifelse(is.na(death_rate), "white", pal(death_rate))
}

pal <- colorNumeric(c("lightblue", "blue", "darkblue", "purple"), domain = c(0, 100))

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(round(calculateDeathRate(num, police_killings$pop))),
              label = ~paste("State: ", name, "Number Of Deaths: ", num, "Population: ", police_killings$pop, "Death Rate", round(calculateDeathRate(num, police_killings$pop))), 
    dashArray = "10",
    highlightOptions = highlightOptions(
    weight = 1,
    color = "#",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE)) 
## Warning in num/pop: longer object length is not a multiple of shorter object
## length

## Warning in num/pop: longer object length is not a multiple of shorter object
## length
## Warning in pal(death_rate): Some values were outside the color scale and will
## be treated as NA
## Warning in num/pop: longer object length is not a multiple of shorter object
## length
## Warning in pal(death_rate): Some values were outside the color scale and will
## be treated as NA
caption <- paste("<div style='font-size: 10px;'>",
                 "Dataset Source: https://www.kaggle.com/datasets/fivethirtyeight/fivethirtyeight-police-killings-dataset",
                 "<br>Created by: Umair Mohammed",
                 "<br>Date Created: October 25th",
                 "</div>")

information <- paste("<div style='font-size: 10px;'>",
                        "This choropleth observes the rate of death's in each state for every 10000 residents",
                        "</div>")

choropleth <- choropleth %>%
  addLegend(position = "bottomright",
            pal = pal,
            values = ~numOfKills$num,
            title = "<b>Rate Of Death</b>",
            opacity = 1) %>% 
  addControl(html = caption, position = "bottomleft") %>%
  addControl("Rate Of Deaths", position = "topright") %>% 
  addControl(html = information, position = "topright")

choropleth