setwd("C:/Users/mvx13/OneDrive - Texas State University/Hackathon_Rohit/Papers/TRB2025_Papers/Older_Dark/Data")
library(data.table)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
dat0 <- fread("Older_Dark_LL.csv")
dim(dat0)
## [1] 65526 11
## Crash_ID2 Year Prsn_Type_ID Latitude Longitude Crash_Sev_ID
## 1: TX_2017_15515796_2 2017 Driver 29.87968 -95.56910 A
## 2: TX_2017_15516154_1 2017 Driver 29.52178 -98.66351 O
## 3: TX_2017_15516185_1 2017 Driver 30.15289 -97.67056 O
## 4: TX_2017_15517251_1 2017 Driver 31.03011 -97.47050 O
## 5: TX_2017_15517363_1 2017 Driver 31.64929 -96.79986 O
## 6: TX_2017_15517451_1 2017 Driver 32.65604 -96.82317 B
## Prsn_Injry_Sev_ID Prsn_Ethnicity_ID Prsn_Age1 Prsn_Injry_Sev_ID1
## 1: O Hispanic 65-74 years O
## 2: O White 65-74 years O
## 3: O Hispanic 65-74 years O
## 4: O White 65-74 years O
## 5: O White 65-74 years O
## 6: B Black 65-74 years BC
## Crash_Sev_ID1
## 1: KA
## 2: O
## 3: O
## 4: O
## 5: O
## 6: BC
Ugly Plot
## Warning: package 'hexbin' was built under R version 4.3.3
ggplot(dat0, aes(x=Latitude, y=Longitude)) + geom_hex(bins=100) +
facet_wrap(~ Year, nrow = 2)+theme_bw(base_size=14)+
scale_fill_gradientn(colours=c("#D3DDDC","blue"),name = "Frequency",na.value=NA)

Moderate Ugly Plot
library(hexbin)
library(tidyverse)
## Warning: package 'stringr' 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.1
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::between() masks data.table::between()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks data.table::first()
## ✖ lubridate::hour() masks data.table::hour()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks data.table::last()
## ✖ lubridate::mday() masks data.table::mday()
## ✖ lubridate::minute() masks data.table::minute()
## ✖ lubridate::month() masks data.table::month()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ lubridate::second() masks data.table::second()
## ✖ purrr::transpose() masks data.table::transpose()
## ✖ lubridate::wday() masks data.table::wday()
## ✖ lubridate::week() masks data.table::week()
## ✖ lubridate::yday() masks data.table::yday()
## ✖ lubridate::year() masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
## Warning: package 'ggspatial' was built under R version 4.3.3
## Warning: package 'tidygeocoder' was built under R version 4.3.3
##
## Attaching package: 'maps'
##
## The following object is masked from 'package:purrr':
##
## map
## library(tmap)
state_map_data <- map('state', fill = TRUE, plot = FALSE) %>%
st_as_sf()
ggplot() +
geom_sf(data = state_map_data) +
geom_point(data = dat0, aes(x = Longitude, y = Latitude)) +
coord_sf(xlim = c(-106.5, -93.6), ylim = c(25.5,36.2))+
facet_wrap(~ Year, nrow = 2)+theme_bw(base_size=14)+
scale_fill_gradientn(colours=c("#D3DDDC","blue"),
name = "Frequency",na.value=NA)

CGPT01
## prompt: Make the x-axis label angle in the code:
ggplot() +
geom_sf(data = state_map_data) +
geom_point(data = dat0, aes(x = Longitude, y = Latitude)) +
coord_sf(xlim = c(-106.5, -93.6), ylim = c(25.5, 36.2)) +
facet_wrap(~ Year, nrow = 2) +
theme_bw(base_size = 14) +
scale_fill_gradientn(colours = c("#D3DDDC", "blue"), name = "Point Distribution", na.value = NA) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

CGPT02
## prompt: Make the points blurry and heatmap like
ggplot() +
geom_sf(data = state_map_data) +
stat_density2d(data = dat0, aes(x = Longitude, y = Latitude, fill = ..level..), geom = "polygon", alpha = 0.5) +
geom_point(data = dat0, aes(x = Longitude, y = Latitude), alpha = 0.3, color = "blue") +
coord_sf(xlim = c(-106.5, -93.6), ylim = c(25.5, 36.2)) +
facet_wrap(~ Year, nrow = 2) +
theme_bw(base_size = 14) +
scale_fill_gradientn(colours = c("#D3DDDC", "blue"), name = "Density", na.value = NA) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

CGPT02 (by Seve)
ggplot() +
geom_sf(data = state_map_data) +
stat_density2d(data = dat0, aes(x = Longitude, y = Latitude, fill = ..level..), geom = "polygon", alpha = 0.5) +
geom_point(data = dat0, aes(x = Longitude, y = Latitude), alpha = 0.3, color = "blue") +
coord_sf(xlim = c(-106.5, -93.6), ylim = c(25.5, 36.2)) +
facet_wrap(~ Crash_Sev_ID, nrow = 2) +
theme_bw(base_size = 14) +
scale_fill_gradientn(colours = c("#D3DDDC", "blue"), name = "Density", na.value = NA) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

CGPT02 (by Seve)
## Prompt: Sort the plot by K, A, B, C, O in 'Crash_Sev_ID'
library(ggplot2)
library(dplyr)
# Assuming dat0 is your dataframe
dat0 <- dat0 %>%
mutate(Crash_Sev_ID = factor(Crash_Sev_ID, levels = c('K', 'A', 'B', 'C', 'O')))
dat0 <- dat0 %>%
mutate(Crash_Sev_ID = factor(Crash_Sev_ID, levels = c('K', 'A', 'B', 'C', 'O')))
ggplot() +
geom_sf(data = state_map_data) +
stat_density2d(data = dat0, aes(x = Longitude, y = Latitude, fill = ..level..), geom = "polygon", alpha = 0.5) +
geom_point(data = dat0, aes(x = Longitude, y = Latitude), alpha = 0.5, color = "#798E87", size = 0.3) +
coord_sf(xlim = c(-106.5, -93.6), ylim = c(25.5, 36.2)) +
facet_wrap(~ Crash_Sev_ID, nrow = 2) +
theme_bw(base_size = 14) +
scale_fill_gradientn(colours = c("#CDC08C", "#9C964A"), name = "Density", na.value = NA) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
