Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'lubridate' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.2 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.2 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.1
── 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(readxl)library(plotly)
Warning: package 'plotly' was built under R version 4.3.2
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
census_api_key("")
To install your API key for use in future sessions, run this function with `install = TRUE`.
teen <-read_csv("data-table.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.
teen |>filter(YEAR ==2021) |>mutate(NAME =abbr2state(STATE)) -> teen_states
states <-get_acs(geography ="state", # gets state by state datavariables ="B01003_001", # this is state populationgeometry =TRUE, # gets geometry (the maps)shift_geo = T) # shifts Hawaii and Alaska
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.
Warning: • You have not set a Census API key. Users without a key are limited to 500
queries per day and may experience performance limitations.
ℹ For best results, get a Census API key at
http://api.census.gov/data/key_signup.html and then supply the key to the
`census_api_key()` function to use it throughout your tidycensus session.
This warning is displayed once per session.
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()
This is all stats stuff and setting up the data.
states |>inner_join(teen_states) |>ggplot() +geom_sf(aes(fill = RATE)) +coord_sf(datum =NA) +scale_fill_viridis_c(labels =label_comma()) +theme_minimal() +labs(title ="Teen Birth Rate by State")
Joining with `by = join_by(NAME)`
This is a chloropleth map of the rate of teen pregnancy/birth by state. The more yellow the color, the higher the rate.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
This is a histogram of the teen birth rate nationally. Note to Dr. M- any time I adjusted the binwidth it looked pretty ugly; the automatic width looked the best.
states_leaflet <-get_acs(geography ="state", # gets state by state datavariables ="B19013_001", # this is state incomegeometry =TRUE, # gets geometry (the maps)shift_geo = F) # we're not shifting Hawaii and Alaska!
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)`.
# A tibble: 21 × 3
offense_type offense_against n
<chr> <chr> <int>
1 all other offenses other 14
2 drug equipment violations society 5
3 simple assault persons 5
4 driving under the influence society 4
5 other burglary/breaking & entering property 4
6 shoplifting property 4
7 all other larceny property 3
8 disorderly conduct society 3
9 drug/narcotic violations society 3
10 theft from building property 3
# ℹ 11 more rows
This is a table of the offenses on 10/24/2022 by type and who the offense was against.