library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(scales)
Warning: package 'scales' was built under R version 4.2.3

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
library(tidycensus)      
Warning: package 'tidycensus' was built under R version 4.2.3
library(sf)              
Warning: package 'sf' was built under R version 4.2.3
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(leaflet)         
Warning: package 'leaflet' was built under R version 4.2.3
library(usdata)   
Warning: package 'usdata' was built under R version 4.2.3
library(plotly)
Warning: package 'plotly' was built under R version 4.2.3

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(tigris)
Warning: package 'tigris' was built under R version 4.2.3
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.

PART ONE

states <- get_acs(geography = "state",               
                  variables = "B01003_001",          
                  geometry = TRUE,                   
                  shift_geo = T)                     
Getting data from the 2018-2022 5-year ACS
Warning: The `shift_geo` argument is deprecated and will be removed in a future
release. We recommend using `tigris::shift_geometry()` instead.
Using feature geometry obtained from the albersusa package
Please note: Alaska and Hawaii are being shifted and are not to scale.
old-style crs object detected; please recreate object with a recent sf::st_crs()
homicide <- read.csv("data-table.csv")
homicide_state <- homicide |> 
  filter(YEAR == 2021) |> 
  mutate(NAME = abbr2state(STATE)) 
states |> 
  inner_join(homicide_state) |> 
  ggplot() + 
  geom_sf(aes(fill = RATE)) + 
  coord_sf(datum = NA)+
  scale_fill_viridis_c(labels = label_comma())+
  theme_minimal()+
  labs(title = "2021 Homicide Rates Per 100,000 By State")
Joining with `by = join_by(NAME)`

states |> 
  inner_join(homicide_state) %>% 
  ggplot(aes(x = estimate)) +
  geom_histogram(binwidth = 5000000)+
  scale_x_continuous(labels = comma)+
  theme_minimal()+
  xlab("Homicide Rates")
Joining with `by = join_by(NAME)`

options(tigris_use_cache = TRUE)
states_leaflet <- get_acs(geography = "state",       
                  variables = "B01003_001",          
                  geometry = TRUE,
                  shift_geo = F) 
Getting data from the 2018-2022 5-year ACS
homicide_colors <- colorNumeric(palette = "viridis", domain = homicide_state$RATE)

states_leaflet %>% 
  inner_join(homicide_state) |>
  leaflet() %>% 
  addTiles() %>% 
  addPolygons(weight = 1, 
              fillColor = ~homicide_colors(RATE), 
              label = ~paste0(NAME, ", Homicide Rate = ", RATE), 
              highlight = highlightOptions(weight = 2)) %>%
  setView(-95, 40, zoom = 4) %>% 
  addLegend(pal = homicide_colors, values = ~RATE)
Joining with `by = join_by(NAME)`
Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
homicide_state |> 
  plot_ly(x = ~RATE) |> 
  add_histogram()

PART TWO

crime_open_database <- readRDS("crime_open_database_core_all_2022.Rds")
chicago_crime <- crime_open_database %>% 
  filter (city_name == "Chicago") %>% 
  filter (location_type == "gas station") %>% 
  filter (offense_against == "society")
chicago_crime %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(stroke = F, fillOpacity = .6, radius = 5, label = ~offense_type)
Assuming "longitude" and "latitude" are longitude and latitude, respectively
chicago_crime %>% 
  count(offense_type, offense_group) %>% 
  arrange(-n)
# A tibble: 6 × 3
  offense_type                                               offense_group     n
  <chr>                                                      <chr>         <int>
1 weapon law violations                                      weapon law v…   139
2 trespass of real property                                  trespass of …    94
3 drug/narcotic violations                                   drug/narcoti…    65
4 liquor law violations (except driving under the influence… liquor law v…     1
5 operating/promoting/assisting gambling                     gambling off…     1
6 prostitution                                               prostitution…     1