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(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(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) # gets census data that we can use to create maps
Warning: package 'tidycensus' was built under R version 4.2.3
library(sf) # helper package for mapping
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) # interactive mapping package
Warning: package 'leaflet' was built under R version 4.2.3
library(usdata) # this package has a conversion utility for state abbreviations to full names
Warning: package 'usdata' was built under R version 4.2.3
Life Expectancy of Each State
Firstly, we will obtain state-by-state data from the tidycensus package.
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.
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()
The dataset I am using is the “Life Expectancy at Birth, by State” dataset from the Stats of the States website. This dataset should show the average life expectancy of each state. The following code imports the dataset and selects only the year 2020:
states |>inner_join(expect2020) |>ggplot() +geom_sf(aes(fill = RATE)) +coord_sf(datum =NA) +theme_minimal() +scale_fill_viridis_c(labels =label_comma()) +labs(title ="2020 Life Expectancy")
Joining with `by = join_by(NAME)`
Looks like a majority of Southern states have a lower life expectancy compared to a majority of Northern states, with the exception being Hawaii. The following histogram shows the average life expectancy of all states:
expect2020 |>ggplot(aes(x = RATE)) +geom_histogram() +scale_x_continuous(labels = comma) +theme_minimal() +xlab("2020 Life Expectancy in All States")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
I personally prefer using the leaflet() command, as you can personally look at each states’ data. Here is a different choropleth of the same dataset using leaflet:
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)`.
Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
Lastly, this is a plotly histogram to accompany the data:
expect2020 |>plot_ly(x =~RATE) |>add_histogram()
Police Shootings in Montana
The following dataset was obtained from The Washington Post’s Fatal Police Shootings dataset. Using this dataset, we will specifically look at all cases in Montana.
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Warning in validateCoords(lng, lat, funcName): Data contains 11 rows with
either missing or invalid lat/lon values and will be ignored
If you hover over each case, it should tell you the threat that police was facing at the time, including the suspect directly attacking the officer (attack). Here is a count of how many threats of each category were shot down: