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(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
library(ggplot2)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
PART 1
census_api_key("")
To install your API key for use in future sessions, run this function with `install = TRUE`.
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()
Drug_overdose<-read_csv('drug overdose.csv')
Rows: 500 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.
Showing drug overdose state by state using choropleth as one can see West Virginia has the highest drug overdose rate by far
states <-get_acs(geography ="state", variables ="B19013_001", shift_geo = T, geometry =TRUE)
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(Drug_overdose) |>ggplot() +geom_sf(aes(fill = RATE)) +coord_sf(datum =NA) +theme_minimal() +scale_fill_viridis_c(labels =label_comma()) +labs(title ="Median State Drug Overdose")
Joining with `by = join_by(NAME)`
Histogram of state to state drug overdose again in this histogram you see an outliar which would show that West Virginia has the highest drug overdose rate by about 25%
Choropleth map using leafplot of Drug Overdose state to state this shows the same above just with a nicer map
options(tigris_use_cache =TRUE)
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!
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
PART 2
Below there is a map using leaflet looking at the crimes committed on April 20th in Austin Texas and below that is a map of one specific crime based on latitute and longitude which was a crime committed at someones office. Below that you will find a dot map, mapping out all the crime locations for April 20th in Austin Texas.