MAPS

library(tidyverse)
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 data
                  variables = "B01003_001",          # this is state population
                  geometry = 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.
Drug_overdose |>
  filter(YEAR == 2021) |>
  mutate(NAME = abbr2state(STATE)) -> Drug_overdose
  1. 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)`

  1. 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%
Drug_overdose |> 
  ggplot(aes(x = RATE)) +
  geom_histogram(binwidth = 10) +
  scale_x_continuous(labels = comma) +
  theme_minimal() +
  xlab("State Drug Overdose")

  1. 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 data
                  variables = "B19013_001",          # this is state income
                  geometry = TRUE,                   # gets geometry (the maps)
                  shift_geo = F)                    # we're not shifting Hawaii and Alaska!
Getting data from the 2018-2022 5-year ACS
drug_overdose_colors <- colorNumeric(palette = "viridis", domain = Drug_overdose$RATE) 

states_leaflet %>%  
  inner_join(Drug_overdose) |>
  leaflet() |>
  addTiles() |>
  addPolygons(weight = 1, fillColor = ~drug_overdose_colors(RATE)) |>
  setView(-95, 40, zoom = 4) |> 
  addLegend(pal = drug_overdose_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'
  1. Histogram using Plotly of states and Drug Overdose again showing that there is an outliar which is West Virginia by about 25%
Drug_overdose |> 
  ggplot(aes(x = RATE)) +
  geom_histogram()
`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.

crime_open_database_core_all_2022 <- readRDS("crime_open_database_core_all_2022.Rds")

Leafplot

crime_open_database_core_all_2022 |> 
  filter(city_name == "Austin") |> 
  filter(month(date_single) == 7) |> 
  filter(day(date_single) == 4) -> april_20


april_20
# A tibble: 222 × 14
       uid city_name offense_code offense_type     offense_group offense_against
     <int> <chr>     <chr>        <chr>            <chr>         <chr>          
 1 1512631 Austin    23F          theft from moto… larceny/thef… property       
 2 1512632 Austin    90Z          all other offen… all other of… other          
 3 1512633 Austin    370          pornography/obs… pornography/… society        
 4 1512634 Austin    22U          other burglary/… burglary/bre… property       
 5 1512635 Austin    290          destruction/dam… destruction/… property       
 6 1512636 Austin    23F          theft from moto… larceny/thef… property       
 7 1512637 Austin    290          destruction/dam… destruction/… property       
 8 1512638 Austin    13A          aggravated assa… assault offe… persons        
 9 1512639 Austin    35A          drug/narcotic v… drug/narcoti… society        
10 1512640 Austin    90D          driving under t… driving unde… society        
# ℹ 212 more rows
# ℹ 8 more variables: date_single <chr>, longitude <dbl>, latitude <dbl>,
#   location_type <chr>, location_category <chr>, census_block <chr>,
#   date_start <chr>, date_end <chr>
leaflet() |> 
  addTiles() |>   
  addMarkers(lat = 30.3, lng = -97.7)
april_20 |> 
  leaflet() |> 
  addTiles() %>% 
  addCircleMarkers(stroke = F, fillOpacity = .6, radius = 5)
Assuming "longitude" and "latitude" are longitude and latitude, respectively