── 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)
library (sf)
Warning: package 'sf' was built under R version 4.0.5
Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
library (leaflet)
library (usdata)
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
Teen <- read_csv ("~/Documents/untitled folder/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 shows the amount of teen pregnancy’s in each state during 2021.
state_pregnancy %>%
ggplot (aes (x = RATE)) +
geom_histogram (binwidth = 1 ) +
scale_x_continuous (labels = comma) +
theme_minimal () +
labs (title = "Teen Pregnancy in 2021" )
The histogram shows the amount of teen pregnancy in the states.
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%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 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%
|
|=================================================================== | 96%
|
|===================================================================== | 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 map above was created using leaflet and it shows the amount of teen pregnancies in each state during 2021.
state_pregnancy %>%
plot_ly (x = ~ RATE) %>%
add_histogram ()
Above is a histogram showing the rate of teen pregnancy in the United States during 2021.
Aurora <- readRDS ("~/Documents/untitled folder/wb225bk3255_co_aurora_2023_01_26.rds" )
Aurora %>%
filter (violation == "Failed to Obey Seat Belt Requirements Adult" )
# A tibble: 8,129 × 16
raw_row_number date time location lat lng geocode_source
<chr> <date> <time> <chr> <dbl> <dbl> <chr>
1 37 2012-01-02 09:25 13700 E 6TH AVE 39.7 -105. GM
2 87 2012-01-02 14:05 S PARKER RD AT E… 39.6 -105. GM
3 86 2012-01-02 14:05 S PARKER RD AT E… 39.6 -105. GM
4 134 2012-01-03 01:56 13600 BLOCK E AL… 39.7 -105. GM
5 163 2012-01-03 08:35 S AIRPORT BLVD A… 39.7 -105. GM
6 203 2012-01-03 10:25 2700 BLOCK S CHA… 39.7 -105. GM
7 230 2012-01-03 12:55 14900 BLOCK E AR… 39.6 -105. GM
8 346 2012-01-04 10:20 15500 BLOCK E I7… 39.7 -105. SU
9 451 2012-01-04 15:10 9500 BLOCK E JEF… 39.7 -105. GM
10 454 2012-01-04 15:15 2700 BLOCK N CHA… 39.8 -105. GM
# ℹ 8,119 more rows
# ℹ 9 more variables: subject_age <int>, subject_race <fct>, subject_sex <fct>,
# type <fct>, violation <chr>, citation_issued <lgl>, outcome <fct>,
# raw_ethnicity <chr>, raw_race <chr>
The code above filtered police stops to only include failure to obey seat-belt research for an adult.
Aurora %>%
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 7928 rows with
either missing or invalid lat/lon values and will be ignored
Above is a dotmap of traffic stops from the Aurora, Colorado Police Department that resulted in tickets for: Failure to Obey Seat Belt Requirements Adult.
Aurora %>%
count (raw_race == "HISPANIC OR LATINO" , subject_sex == "female" )
# A tibble: 6 × 3
`raw_race == "HISPANIC OR LATINO"` `subject_sex == "female"` n
<lgl> <lgl> <int>
1 FALSE FALSE 146153
2 FALSE TRUE 93566
3 FALSE NA 2008
4 TRUE FALSE 9783
5 TRUE TRUE 6088
6 TRUE NA 8
This table shows that out of the 257,606 people pulled over in Aurora, Colorado 6,088 of them were Hispanic/Latina females.