library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
library(paletteer)
library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
library(tidycensus)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(rnaturalearth)
## Support for Spatial objects (`sp`) will be deprecated in {rnaturalearth} and will be removed in a future release of the package. Please use `sf` objects with {rnaturalearth}. For example: `ne_download(returnclass = 'sf')`
library(dplyr)
df<-read.csv('FATAL ENCOUNTERS DOT ORG SPREADSHEET (See Read me tab) - Form Responses.csv')

1. correlatoin between hignest level of force and whether individual flees

unique(df$Fleeing.Not.fleeing) 
##  [1] ""                                          
##  [2] "Not fleeing"                               
##  [3] "Duplicate of 13419?"                       
##  [4] "Duplicate of 13457?"                       
##  [5] "Fleeing/Motorcycle"                        
##  [6] "Fleeing/Vehicle"                           
##  [7] "Fleeing/Foot"                              
##  [8] "Uncertain"                                 
##  [9] "Fleeing/vehicle"                           
## [10] "Fleeing/Unspecified"                       
## [11] "Vehicle/Motorcycle"                        
## [12] "Vehicle/Fleeing"                           
## [13] "Fleeing/Veh"                               
## [14] "Vehicle"                                   
## [15] "Fleeing/Bicycle"                           
## [16] "Fleeing/Skateboard"                        
## [17] "Fleeling/Vehicle"                          
## [18] "Not Fleeing"                               
## [19] "Uncertain/Police alleged vehicular assault"
## [20] "None"                                      
## [21] "Motorcycle"                                
## [22] "Foot"                                      
## [23] "Firearm/Handgun"                           
## [24] "Fleeing/foot"                              
## [25] "Fleeing/Uncertain"                         
## [26] "Duplicate of 9914"
# replace value that contains "duplicate" with that of the corresponding row.
for (i in 1:nrow(df)) {
  if (grepl("Duplicate of", df$Fleeing.Not.fleeing[i])) {
    dupid <- as.numeric(sub("Duplicate of ", "", df$Fleeing.Not.fleeing[i]))
    replacement <- df$Fleeing.Not.fleeing[df$Unique.ID == dupid]
    if (length(replacement) > 0) {
      df$Fleeing.Not.fleeing[i] <- replacement
    }
  }
}
## Warning: 强制改变过程中产生了NA
## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数
## Warning: 强制改变过程中产生了NA
## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数

## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数
unique(df$Fleeing.Not.fleeing) 
##  [1] ""                                          
##  [2] "Not fleeing"                               
##  [3] NA                                          
##  [4] "Fleeing/Motorcycle"                        
##  [5] "Fleeing/Vehicle"                           
##  [6] "Fleeing/Foot"                              
##  [7] "Uncertain"                                 
##  [8] "Fleeing/vehicle"                           
##  [9] "Fleeing/Unspecified"                       
## [10] "Vehicle/Motorcycle"                        
## [11] "Vehicle/Fleeing"                           
## [12] "Fleeing/Veh"                               
## [13] "Vehicle"                                   
## [14] "Fleeing/Bicycle"                           
## [15] "Fleeing/Skateboard"                        
## [16] "Fleeling/Vehicle"                          
## [17] "Not Fleeing"                               
## [18] "Uncertain/Police alleged vehicular assault"
## [19] "None"                                      
## [20] "Motorcycle"                                
## [21] "Foot"                                      
## [22] "Firearm/Handgun"                           
## [23] "Fleeing/foot"                              
## [24] "Fleeing/Uncertain"
# deal with some NA
df$Fleeing.Not.fleeing[df$Fleeing.Not.fleeing == ""] <- NA
df$Highest.level.of.force[df$Highest.level.of.force == ""] <- NA

# subset df to 'flee' and 'notflee'

notflee=c("Not fleeing","Not Fleeing")
flee=c("Fleeing/Motorcycle","Fleeing/Vehicle","Fleeing/Foot" ,"Uncertain","Fleeing/vehicle","Fleeing/Unspecified","Vehicle/Motorcycle","Vehicle/Fleeing","Fleeing/Veh","Vehicle","Fleeing/Bicycle" ,"Fleeing/Skateboard","Fleeling/Vehicle","Motorcycle","Foot","Firearm/Handgun","Fleeing/foot","Fleeing/Uncertain")

df_flee <- df[df$Fleeing.Not.fleeing %in% flee, ]
df_notflee<-df[df$Fleeing.Not.fleeing %in% notflee,]
nrow(df)
## [1] 31498
nrow(df_flee)
## [1] 6169
nrow(df_notflee)
## [1] 10906
# show pie chart of highest level of force by flee or not

count1<-as.data.frame(table(df_flee$Highest.level.of.force))
count1$Per <- percent(count1$Freq / sum(count1$Freq),accuracy = 1)
count2<-as.data.frame(table(df_notflee$Highest.level.of.force))
count2$Per <- percent(count2$Freq / sum(count2$Freq),accuracy = 1)

p_flee <- ggplot(count1, aes(x = "", y = Freq,fill=Var1)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y") +
  labs(title = "Highest level of force when flee",fill='highest level of force',x='',y='') +
  geom_text(aes(label = Per),position = position_stack(vjust = 0.5))


p_notflee<-ggplot(count2, aes(x = "", y = Freq,fill=Var1)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y") +
  labs(title = "Highest level of force when not flee",fill='highest level of force',x='',y='')+
  geom_text(aes(label = Per),position = position_stack(vjust = 0.5))

# 打印图表
print(p_flee)

print(p_notflee)

From above, when individule flee, 57% of highest level of force are gunshot, 38% are vehicle. When individule not flee, 81% of highest level of force are gun shot, and 11% are vehicle. It suggest that, when individual flees, the police had a lower chance of shooting gun.

2. correlatoin between highest level of force and year.

# prepare data
#get year
colnames(df)[colnames(df) == "Date.of.injury.resulting.in.death..month.day.year."] <- "date"
df$year=substr(df$date,7,10)

# summary data
df_summary1 <- df %>%
  group_by(year, Highest.level.of.force) %>%
  summarise(count = n()) %>%
  ungroup()
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
df_summary1<-df_summary1%>%filter(!is.na(Highest.level.of.force))

# create line gragh
color_palette <- paletteer_d("ggthemes::Classic_20")
ggplot(df_summary1, aes(x = year, y = count, group = Highest.level.of.force, color = Highest.level.of.force)) +
  geom_line() +
  labs(title ="Number of different types of highest force by year", x = "Year", y = "Count") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  scale_color_manual(values = color_palette)

#### From above, the graph shows that the overall number of death caused by police is generally on the rise. Among all the different levels of force, gunshot has always been the No.1, much more ahead of the second, vehicle. Moreover, the growth rate of “Gunshot” is also the first.

3. Correlation between highest level of force and race.

# summary data

df_summary2 <- df %>%
  group_by(Race, Highest.level.of.force) %>%
  summarise(count = n()) %>%
  ungroup()
## `summarise()` has grouped output by 'Race'. You can override using the
## `.groups` argument.
df_summary2 <- df_summary2[df_summary2$Race != "Christopher Anthony Alexander", ]

errow<-df_summary2[grepl("imputed", df_summary2$Race), ]
df_summary2$count[df_summary2$Race=='African-American/Black']<- df_summary2$count[df_summary2$Race=='African-American/Black'] + errow$count
df_summary2 <- df_summary2[!grepl("imputed", df_summary2$Race), ]
# draw graphs
ggplot(df_summary2, aes(x =Highest.level.of.force, y = count, fill = Race)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(x = "Highest level of Force", y = "Count") +
  scale_fill_discrete(name = "Race")

#### From above, the barplot indicates that European-American acounts for the largest number of ‘Gunshot’ and “Vehicle”,followed by Black race and Hispanic race.

4. Correlation beteewn highest level of force and gender.

df_summary3 <- df %>%
  group_by(Gender, Highest.level.of.force) %>%
  summarise(count = n()) %>%
  ungroup()
## `summarise()` has grouped output by 'Gender'. You can override using the
## `.groups` argument.
df_summary3$Gender[df_summary3$Gender == ""] <- NA
df_summary3$Highest.level.of.force[df_summary3$Highest.level.of.force == ""] <- NA
df_summary3<-df_summary3%>%filter(!is.na(Highest.level.of.force))
df_summary3<-df_summary3%>%filter(!is.na(Gender))

df_summary3$count<-as.numeric(df_summary3$count)
ggplot(df_summary3, aes(x = Highest.level.of.force, y = count, fill = Gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Highest level of force", y = "count") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot(df_summary3, aes(x = Highest.level.of.force, y = 1000, fill = Gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Highest level of force", y = "count") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#### From above, the proportion of male is much bigger than that of Femial and Transgender when it comes to the highest level of force. Male outnumbers Female in the vast majority of cases.