R Markdown
library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ purrr 1.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.1 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.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(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(ggmap)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(ggplot2)
Traffic_Crashes_Crashes <- read_csv(
"C:/Users/mhe29/OneDrive - Drexel University/CJS 310 R Files/Traffic_Crashes_-_Crashes.csv"
)
## Rows: 1030368 Columns: 48
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (31): CRASH_RECORD_ID, CRASH_DATE_EST_I, CRASH_DATE, TRAFFIC_CONTROL_DEV...
## dbl (17): POSTED_SPEED_LIMIT, LANE_CNT, STREET_NO, BEAT_OF_OCCURRENCE, NUM_U...
##
## ℹ 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.
Traffic_Crashes_Crashes <- Traffic_Crashes_Crashes %>%
mutate(CRASH_YEAR = year(mdy_hms(CRASH_DATE)))
crashes_subset <- Traffic_Crashes_Crashes %>%
filter(!is.na(LATITUDE) & !is.na(LONGITUDE)) %>%
filter(CRASH_YEAR == 2024)
register_stadiamaps(key="86281c70-b939-46e0-88df-5eea61f839d2")
chicago_bbox <- c(left = -87.85, bottom = 41.64, right = -87.52, top = 42.02)
chicago_map <- get_stadiamap(
bbox = chicago_bbox,
zoom = 11,
maptype = "stamen_toner_lite"
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(chicago_map) +
stat_density_2d(data = crashes_subset,
aes(x = LONGITUDE, y = LATITUDE,
fill = after_stat(level),
alpha = after_stat(level)),
linewidth = 0.01,
bins = 35,
geom = "polygon") +
scale_fill_gradient(low = "yellow", high = "red") +
scale_alpha(range = c(0.1, 0.5), guide = "none") +
labs(title = "2024 Chicago Traffic Crash Hotspots",
subtitle = "Updated syntax to remove deprecation warnings",
x = "Longitude",
y = "Latitude",
fill = "Intensity") +
theme_minimal()
## Warning: Removed 371 rows containing non-finite outside the scale range
## (`stat_density2d()`).
