Map Assignment GR Stats

library(tidyverse)
── 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)

Attaching package: 'scales'

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

    discard

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

    col_factor
library(tidycensus)
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(leaflet)
library(usdata)
census_api_key("029d1b156c08e4a6d6d3709198185e57a664b766")
To install your API key for use in future sessions, run this function with `install = TRUE`.
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
library(readr)
Teen <- read_csv("Desktop/GR Stats/Teen.csv")
Rows: 450 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): STATE, URL
dbl (2): YEAR, RATE

ℹ 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.
state_pregnancy <-Teen %>% 
  filter(YEAR == 2021) %>% 
  mutate(NAME = abbr2state(STATE))
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()
states %>% 
  inner_join(state_pregnancy) %>% 
  ggplot() +
  geom_sf(aes(fill = RATE)) +
  coord_sf(datum = NA) +
  theme_minimal() +
  scale_fill_viridis_c(labels = label_comma()) +
  labs(title = "Teen Pregnancy in 2021")
Joining with `by = join_by(NAME)`

The map above displays the quantity of teen-aged pregnancies per state in 2021. From this map we can see that the Southeast region of the United States has the highest amount of teen pregnancies in 2021.

state_pregnancy %>% 
  ggplot(aes(x = RATE)) +
  geom_histogram(binwidth = 1) +
  scale_x_continuous(labels = comma) +
  theme_minimal() +
  labs(title = "Teen Pregnancy in 2021")

This histogram graph furthermore shows the distribution of teen pregnancies per state in the year 2021.

teen_leaflet <- get_acs(geography = "state", 
                        variables = "B19013_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%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
teen_colors <- colorNumeric(palette = "viridis", domain = state_pregnancy$RATE)
teen_leaflet %>% 
  inner_join(state_pregnancy) %>% 
  leaflet() %>% 
  addTiles() %>% 
  addPolygons(weight = 1, 
              fillColor = ~teen_colors(RATE),
              label = ~paste0(NAME, ", Teen Pregnant = ", RATE), highlight = highlightOptions(weight = 2)) %>% 
  setView(-95, 40, zoom = 4) %>% 
  addLegend(pal = teen_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'

The leaflet map above displays an interactive method to show the quantity of teen pregnancies per state in 2021. Furthermore, we see that Arkansas has the hgihest rate of teen pregnancies (26.5%), and following close behind is Mississippi (25.6%). The state with the lowest rate of teen pregnancies in 2021 was the state of New Hampshire with a rate of 5.4%.

state_pregnancy %>% 
  plot_ly(x = ~RATE) %>% 
  add_histogram()

This histogram graph above shows the common rates of teen pregnancies per state in the year 2021.

library(readr)
mt_statewide_2023_01_26 <- read_csv("Desktop/GR Stats/mt_statewide_2023_01_26.csv")
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 921228 Columns: 30
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (18): location, county_name, subject_race, subject_sex, department_name...
dbl   (5): raw_row_number, lat, lng, subject_age, vehicle_year
lgl   (5): arrest_made, citation_issued, warning_issued, frisk_performed, se...
date  (1): date
time  (1): time

ℹ 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.
mt_statewide_2023_01_26 %>%
  filter(violation == "SPEED")
# A tibble: 29 × 30
   raw_row_number date       time     location             lat   lng county_name
            <dbl> <date>     <time>   <chr>              <dbl> <dbl> <chr>      
 1              3 2009-01-03 11:36:42 P007 HWY 93 MM 77…  46.7 -114. Missoula C…
 2              4 2009-01-04 10:33:11 P007 HWY 93 MM 81…  46.7 -114. Missoula C…
 3              8 2009-01-05 15:32:41 P007 HWY 93 MM 79…  46.7 -114. Missoula C…
 4             55 2009-02-07 14:45:53 INTERSTATE 90 AT …  45.7 -109. Yellowston…
 5             56 2009-02-07 14:55:48 HEY 87/89, RM 84-…  47.5 -111. Cascade Co…
 6             68 2009-02-20 11:00:27 MM261               45.9 -112. Jefferson …
 7             69 2009-02-21 10:23:23 HWY 87/200, RM 4-…  47.3 -111. Cascade Co…
 8             89 2009-03-09 15:55:44 US 93 35 MM NB      47.4 -114. Lake County
 9             93 2009-03-15 16:16:56 INTERSTATE 90 @ M…  45.6 -109. Stillwater…
10             99 2009-03-21 14:57:43 MT 191 MP 26        46.8 -110. Fergus Cou…
# ℹ 19 more rows
# ℹ 23 more variables: subject_age <dbl>, subject_race <chr>,
#   subject_sex <chr>, department_name <chr>, type <chr>, violation <chr>,
#   arrest_made <lgl>, citation_issued <lgl>, warning_issued <lgl>,
#   outcome <chr>, frisk_performed <lgl>, search_conducted <lgl>,
#   search_basis <chr>, reason_for_stop <chr>, vehicle_make <chr>,
#   vehicle_model <chr>, vehicle_type <chr>, …

The dataset I’ve chosen is based out of the state of Montana. this dataset initially looked at traffic stops and the reasons the traffic stops occured. Based on the code above I’ve filtered the dataset to display the number of speeding incidents in the state of Montana.

mt_statewide_2023_01_26 %>%
  leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(stroke = F, fillOpacity = .6, radius = 5)
Assuming "lng" and "lat" are longitude and latitude, respectively
Warning in validateCoords(lng, lat, funcName): Data contains 436 rows with
either missing or invalid lat/lon values and will be ignored

This dot map displays traffic stops that occured in the state of Montana due to speeding as being the reason for the traffic stop.

mt_statewide_2023_01_26 %>%
  count(subject_race == "hispanic", subject_sex == "male")
# A tibble: 8 × 3
  `subject_race == "hispanic"` `subject_sex == "male"`      n
  <lgl>                        <lgl>                    <int>
1 FALSE                        FALSE                   296676
2 FALSE                        TRUE                    606360
3 FALSE                        NA                         115
4 TRUE                         FALSE                     3586
5 TRUE                         TRUE                     14381
6 TRUE                         NA                           4
7 NA                           FALSE                       49
8 NA                           TRUE                        57

This table shows the total amount of traffic stops that occured in the state of Montana due to speeding. Furthermore to narrow down the selection, this table displays the amount of hispanic males that were pulled over for speeding. And in the chart we see that out of the total 903,036 traffic stops that were made, 14,381 were of hispanic males speeding. Which accounts for 1.59% of all traffic stops in the state of Montana. ## Quarto