maps assignment finished

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.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.5.0     ✔ 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)

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.3.3
library(sf)             
Warning: package 'sf' was built under R version 4.3.3
Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(leaflet)        
Warning: package 'leaflet' was built under R version 4.3.3
library(usdata)
Warning: package 'usdata' was built under R version 4.3.3
library(plotly)

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
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()
suicide_rates <- read_csv("suicide_rates.csv")
Rows: 450 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): STATE, URL
dbl (2): YEAR, RATE
num (1): DEATHS

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
suicide_states <- suicide_rates |> 
  filter(YEAR == 2021) |> 
  mutate(NAME = abbr2state(STATE))
states_leaflet <- get_acs(geography = "state",
                   variables = "B01003_001",     
                   geometry = TRUE,       
                  shift_geo = F) 
Getting data from the 2018-2022 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
austin_stops <- readRDS("crime_open_database_core_austin_2022.rds")
austin_stops  |> 
  filter(month(date_single) == 7) |> 
  filter(day(date_single) == 4) -> july_4
  1. The following is a choropleth map of suicide rates across the United States.
states |> 
  inner_join(suicide_states) |> 
  ggplot() + 
  geom_sf(aes(fill = RATE)) + 
  theme_minimal() + 
  scale_fill_viridis_c(labels = label_comma()) +
  labs(title = "Suicide rates 2021") +
  coord_sf(datum = NA)
Joining with `by = join_by(NAME)`

  1. The following histogram displays suicide rates across all states.
suicide_states |> 
  ggplot(aes(x = RATE)) +
  theme_minimal() +
  scale_x_continuous(labels = comma) + 
  xlab("Median Suicide Rates") +
  geom_histogram(binwidth = 3)

  1. The following code uses leaflet to create a choropleth map displaying suicide rates by state.
suicide_colors <- colorNumeric(palette = "viridis", domain = suicide_states$RATE)
state_colors <- colorNumeric(palette = "viridis", domain = states_leaflet$estimate)

states_leaflet |>
  inner_join(suicide_states) |> 
leaflet() |> 
  addTiles() |>
  addPolygons(weight = 1, fillColor = ~suicide_colors(RATE), label = ~paste0(NAME, ", Suicide Rate = ", RATE), highlight = highlightOptions(weight = 2)) |>
  setView(-95, 40, zoom = 4) |>
  addLegend(pal = suicide_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'

Suicide Rates by State: 2021

  1. The following code uses plotly to create a histogram of suicide rates across the United States.
suicide_states |> 
  plot_ly(x = ~RATE) |> 
  add_histogram()

Suicide Rates Across the United States: 2021

  1. The following choropleth map displays traffic stops made around Austin, Texas on July 4th, 2022.
july_4 |>
leaflet() |> 
  addTiles() |>
  addCircleMarkers(stroke = F, fillOpacity = .6, radius = 5,label = ~offense_type)
Assuming "longitude" and "latitude" are longitude and latitude, respectively
  1. The following table displays offenses as well as who/what they were against in the Austin area on July 4th, 2022.
july_4 |>
  count(offense_type, offense_against)
# A tibble: 26 × 3
   offense_type                                            offense_against     n
   <chr>                                                   <chr>           <int>
 1 aggravated assault                                      persons            37
 2 all other larceny                                       property           24
 3 all other offenses                                      other              12
 4 counterfeiting/forgery                                  property            1
 5 credit card/automated teller machine fraud              property            1
 6 destruction/damage/vandalism of property (except arson) property           17
 7 disorderly conduct                                      society             8
 8 driving under the influence                             society             6
 9 drug equipment violations                               society             1
10 drug/narcotic violations                                society             5
# ℹ 16 more rows