#Set up
fatal <- read.csv('fatal.csv')
#wanted to explore these variables at first
print(table(fatal$State))
##
## AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL
## 1 129 700 391 914 4969 693 142 91 63 1894 1216 116 296 161 1117
## IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH
## 609 428 437 655 215 553 109 903 366 945 446 116 673 43 217 66
## NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT
## 549 444 423 725 1022 721 358 992 32 620 61 695 2797 269 663 42
## WA WI WV WY
## 675 482 190 64
print(table(fatal$Highest.level.of.force))
##
## Asphyxiated/Restrained
## 4 347
## Asphyxiation/Restrain Asphyxiation/Restrained
## 1 2
## Beaten/Bludgeoned with instrument Burned/Smoke inhalation
## 182 45
## Chemical agent/Pepper spray Drowned
## 35 203
## Drug overdose Fell from a height
## 182 82
## Gunshot Less-than-lethal force
## 22238 1
## Medical emergency Other
## 397 65
## Restrain/Asphyxiation Stabbed
## 1 52
## Tasered Undetermined
## 936 101
## Vehicle
## 6624
print(table(fatal$Race))
##
##
## 1
## 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
fatal <- fatal %>%
mutate(Age = as.numeric(Age))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Age = as.numeric(Age)`.
## Caused by warning:
## ! NAs introduced by coercion
fatal_summary <- fatal %>%
group_by(State) %>%
summarise(
avg_age = mean(Age, na.rm = TRUE),
total_cases = n(),
common_race = names(sort(table(Race), decreasing = TRUE))[1],
common_force = names(sort(table(Highest.level.of.force), decreasing = TRUE))[1])
#explore
states_sf <- tigris::states(cb = TRUE) %>%
st_transform(crs = 4326)
## Retrieving data for the year 2024
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
states_sf <- states_sf %>%
left_join(fatal_summary, by = c("STUSPS" = "State"))
map1 <- tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
## This message is displayed once per session.
tm_shape(states_sf) +
tm_polygons(
"total_cases",
palette = "Reds",
style = "jenks",
title = "Total Cases"
) +
tm_layout(
title = "Total Cases by State",
legend.outside = FALSE
)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "jenks"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
## 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.
map1.1 <- tmap_mode("view")
## ℹ tmap modes "plot" - "view"
tm_shape(states_sf) +
tm_polygons(
"total_cases",
palette = "Reds",
style = "quantile",
title = "Total Cases"
) +
tm_layout(
title = "Total Cases by State",
legend.outside = FALSE
)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "quantile"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
## 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.
map2 <- tmap_mode("view")
## ℹ tmap modes "plot" - "view"
tm_shape(states_sf) +
tm_polygons(
"avg_age",
palette = "Reds",
style = "jenks",
title = "Average Age"
) +
tm_layout(
title = "Average Age",
legend.outside = FALSE
)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "jenks"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
## 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.
print(map2)
## [1] "view"
#by intended use
fatal.2 <- read.csv('fatal.csv')
fatal.2 <- fatal.2 %>%
select(State, Intended.use.of.force..Developing.) %>%
group_by(State, Intended.use.of.force..Developing.) %>%
summarise(total = n(), .groups = 'drop')
fatal.2 <- fatal.2 %>%
filter(Intended.use.of.force..Developing. %in% c('Suicide', 'Deadly force', 'Pursuit', 'Less-than-lethal force'))
fatal.3 <- fatal.2 %>%
filter(Intended.use.of.force..Developing. %in% c('Less-than-lethal force'))
states_sf.2 <- tigris::states(cb = TRUE) %>%
st_transform(crs = 4326)
## Retrieving data for the year 2024
states_sf.2 <- states_sf.2 %>%
left_join(fatal.3, by = c("STUSPS" = "State"))
map1.4 <- tmap_mode("view")
## ℹ tmap modes "plot" - "view"
tm_shape(states_sf.2) +
tm_polygons(
"total",
palette = "Reds",
style = "jenks",
title = "deadly force not intended"
) +
tm_layout(
title = "Total Cases of fatality by deadly force not intended by State",
legend.outside = FALSE
)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "jenks"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
## 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.