# Import .csv downloaded from https://docs.google.com/spreadsheets/d/1dKmaV_JiWcG8XBoRgP8b4e9Eopkpgt7FL7nyspvzAsE/edit#gid=0
library(readr)
fatal_raw <- read_csv("FATAL ENCOUNTERS DOT ORG SPREADSHEET (See Read me tab) - Form Responses.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
# head(fatal_raw)
# Load libraries
library(sf)
library(tmap)
library(jsonlite)
library(tidyverse)
library(httr)
library(jsonlite)
library(reshape2)
library(here)
library(knitr)
library(tigris)
# Since I come from China (race-Asian), I want to know about cases where Asians were subjects
# Have a glimpse about column "Race" -- 
table(fatal_raw$Race)
## 
##                                    African-American/Black 
##                                                      7008 
## African-American/Black African-American/Black Not imputed 
##                                                         4 
##                                    Asian/Pacific Islander 
##                                                       485 
##                             Christopher Anthony Alexander 
##                                                         1 
##                 European-American/European-American/White 
##                                                        37 
##                                   european-American/White 
##                                                         1 
##                                   European-American/White 
##                                                     10614 
##                                           Hispanic/Latino 
##                                                      4192 
##                                            Middle Eastern 
##                                                        53 
##                                   Native American/Alaskan 
##                                                       323 
##                                          Race unspecified 
##                                                      8779
# There are 485 cases identified as "Asian/Pacific Islander" related.
# Create an "Asian/Pacific Islander" subset and convert some undesirable column-datatypes 

fatal_asian <- fatal_raw %>% filter(Race=='Asian/Pacific Islander') %>%
  select(c(1:19)) %>% 
  mutate_at('Latitude', as.numeric) %>% 
  mutate_at('Date of injury resulting in death (month/day/year)', 
            ~ as.Date(., format="%m/%d/%Y")) %>% 
  rename_with(make.names)
# Check datatypes
glimpse(fatal_asian)
## Rows: 485
## Columns: 19
## $ Unique.ID                                          <dbl> 31325, 31128, 31139…
## $ Name                                               <chr> "John \"Amoo\" Za M…
## $ Age                                                <dbl> 28, 28, 33, 24, 43,…
## $ Gender                                             <chr> "Male", "Male", "Fe…
## $ Race                                               <chr> "Asian/Pacific Isla…
## $ Race.with.imputations                              <chr> "Asian/Pacific Isla…
## $ Imputation.probability                             <chr> "Not imputed", "Not…
## $ URL.of.image..PLS.NO.HOTLINKS.                     <chr> "https://fatalencou…
## $ Date.of.injury.resulting.in.death..month.day.year. <date> 2021-12-10, 2021-1…
## $ Location.of.injury..address.                       <chr> "Shady Lane Avenue"…
## $ Location.of.death..city.                           <chr> "Pine River", "Vanc…
## $ State                                              <chr> "WI", "WA", "UT", "…
## $ Location.of.death..zip.code.                       <chr> "54452", "98682", "…
## $ Location.of.death..county.                         <chr> "Lincoln", "Clark",…
## $ Full.Address                                       <chr> "Shady Lane Avenue …
## $ Latitude                                           <dbl> 45.13805, 45.65727,…
## $ Longitude                                          <dbl> -89.59039, -122.547…
## $ Agency.or.agencies.involved                        <chr> "Lincoln County She…
## $ Highest.level.of.force                             <chr> "Gunshot", "Gunshot…
# Convert fatal_asian into a sf object for location mapping
fatal_asian_sf <- fatal_asian %>% 
  st_as_sf(coords=c("Longitude", "Latitude"), crs = 4326)
library(leaflet)
library(htmlwidgets)
library(htmltools)

# CSS for title
tag.map.title <- tags$style(HTML("
  .leaflet-control.map_title { 
    position: absolute;
    left: 50px;
    width: 320px;
    text-align: left;
    color: orange;
    padding-left: 10px; 
    background: rgba(20,20,20,0.2);
    font-weight: bold;
    font-size: 20px;
    font-family: Helvetica;
    border-color: white;
    border-radius: 10px;
  }"))

# Format title
title <- tags$div(
  class="map_title", tag.map.title, HTML("<p>Fatal Encounters - Asian/Pacific Islander</p>")
)  

# Label for mouseover & popup
labels <- paste(
  "<strong>Date: </strong>", fatal_asian$Date.of.injury.resulting.in.death..month.day.year.,"<br>",
  "<strong>Age: </strong>", fatal_asian$Age,"<br>",
  "<strong>Agency Involved: </strong>", fatal_asian$Agency.or.agencies.involved,"<br>",
  "<strong>Highest Force: </strong>", fatal_asian$Highest.level.of.force) %>%
  lapply(htmltools::HTML)

factpal <- colorFactor(topo.colors(2), fatal_asian$Gender)

states <- states(cb = T) 
## Retrieving data for the year 2021
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |======================================================================| 100%
st_crs(states) <- 4326
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
leaflet(states) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 1) %>%
  
  # Yelp point
  addCircleMarkers(data = fatal_asian_sf,
                   fillColor= ~factpal(Gender),
                   weight=1,
                   color= ~factpal(Gender),
                   popup= ~labels,
                   label= ~labels) %>% 
  
  # Legend
  addLegend("bottomright", pal = factpal, values = fatal_asian$Gender, title = "Gender", opacity = 1) %>% 
  
  # Title
  addControl(title, position="topleft",className="map_title") %>% 
  setView(-98.5795, 39.8282, zoom=3)

Cases are more densed on north-eastern and south-western coast of the US. Also, the number of cases in mid-east inland are more than the number recorded in mid-west inland. A possibile explanation can be Asian people are mostly living in these areas.

# Conduct a temporal analysis
# Extract year from date
fatal_asian <- fatal_asian %>%
  mutate(Year = year(Date.of.injury.resulting.in.death..month.day.year.))
# Count cases in each year
ggplot(data = fatal_asian) + 
  geom_bar(mapping = aes(x=Year, fill=Gender), position = "stack") +
  labs(x = "Year", 
       y = "No. Fatal Records", 
       title = "Annual Number of Fatal Encounters(Asian/Pacific Islander)")

The recorded cases showing an increasing trend over the past 2 decades. This can because the data are more and more transparent and available, or because the cases are actually increasing. And more males were killed than females.

# Code & ideas borrowed heavily from CÉDRIC SCHERER's personal website: 
# https://www.cedricscherer.com/2021/07/05/a-quick-how-to-on-labelling-bar-graphs-in-ggplot2/

top_forces <- fatal_asian %>% 
  group_by(Highest.level.of.force) %>% 
  tally() %>% 
  arrange(desc(n)) %>% 
  mutate(Highest.level.of.force = factor(Highest.level.of.force, levels = .$Highest.level.of.force[seq(nrow(.),1)])) %>% 
  # Format text label
  mutate(pct = scales::percent(n / sum(n),accuracy = 0.1),
         pct = case_when(row_number() == 1 ~ str_c(pct, " of all forces"), TRUE ~ pct)) %>% 
  # Define aesthetic properties - label location
  mutate(nudge = case_when(row_number()==1 ~ 1.05, TRUE ~ -0.2)) %>% 
  # Define aesthetic properties - color
  mutate(color = case_when(row_number()==1 ~ "gray30", TRUE ~ "gray70")) %>% 
  # Color palette
  mutate(pal = c(rep('gray70', nrow(.)-3), "coral2", "mediumpurple1", "goldenrod1")) %>% 
  # with() is required to be able to call variables with referencing to data frame
  with(
    # ggplot
    ggplot(data = .) +
      # Bars
      geom_col(mapping = aes(y = Highest.level.of.force, 
                             x = n, 
                             fill = Highest.level.of.force)) +
      # Text
      geom_text(mapping = aes(y = Highest.level.of.force, 
                              x = n, 
                              label = pct), 
                # Calling aesthetic properties defined above
                hjust=nudge, color=color, 
                # Font styling
                fontface="bold.italic") + 
      # Stretch x axis
      scale_x_continuous(limits = c(NA, 400)) +
      # Custom palette
      scale_fill_manual(values = pal, guide="none") +
      # Labels
      labs(x = "Count", y = "Forces", title = "Highest Level of Forces\n") +
      # Dark theme
      ggdark::dark_theme_classic()
  )
## Inverted geom defaults of fill and color/colour.
## To change them back, use invert_geom_defaults().
top_forces

More than three quaters of the deaths were attributed, if not caused, by gunshotting. There should be a better “official attempt” to manufacture justifiability…

# Take a look at the distribution of ages of the subjects
# install.packages("ggplot2")

# Histogram with kernel density
# Reference: https://r-charts.com/distribution/histogram-density-ggplot2/#google_vignette
ggplot(fatal_asian %>% filter(!is.na(Age)), 
       aes(x =Age)) + 
  geom_histogram(aes(y = ..density.., fill = Gender),
                 bins = 40,
                 color="black") +
  geom_density() 
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Subjects are mostly aged between 20 to around 55. Color the bins with the gender can be misleading because the bins represent the percentages of the age group instead of the actual numbers.

# Plot histogram with separated genders
ggplot(fatal_asian %>% filter(!is.na(Age)), 
       aes(x =Age)) + 
  geom_histogram(aes(y = ..density..),
                 bins = 30,
                 fill = "skyblue",
                 color="black",
                 position = "dodge") +
  geom_density() +
  facet_wrap(~Gender)

The age distribution of female subjects looks stranger than the age distribution of males, which is more similar to the overall age distribution, which probably because the female samples in this dataset is very small.

# Plot histogram with separated genders using the whole dataset
ggplot(fatal_raw %>% filter(!is.na(Age) & Gender==c("Female","Male")), 
       aes(x =Age)) + 
  geom_histogram(aes(y = ..density..),
                 bins = 40,
                 fill = "skyblue",
                 color="black",
                 position = "dodge") +
  geom_density() +
  facet_wrap(~Gender)

The age distributions of female and male subjects were similar in the full dataset. And similar to the “Asian/Pacific Islander” subset, the subjects are mostly aged between 20 to 60.