library(tidyverse)
## ── 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.5.1     ✔ 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(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(units)
## udunits database from C:/Users/benso/AppData/Local/R/win-library/4.4/units/share/udunits/udunits2.xml
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(leaflet)
library(dbscan)
## 
## Attaching package: 'dbscan'
## 
## The following object is masked from 'package:stats':
## 
##     as.dendrogram
library(sfnetworks)
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tidygraph)
## 
## Attaching package: 'tidygraph'
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(plotly)
## 
## 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(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(here)
## here() starts at C:/Users/benso/Documents/Georgia Tech/CP8883/Final Project
library(tidytransit)
library(tidycensus)
library(leafsync)

epsg <- 4326
gtfs_cta <- read_gtfs("https://www.transitchicago.com/downloads/sch_data/google_transit.zip")
## Warning: Found non .txt files when attempting to read the GTFS feed: developers_license_agreement.htm
## These files have been ignored and were not imported to the GTFS object.
subway_stations <- gtfs_cta$stops %>%
  filter(location_type == 1) %>% 
  select(stop_id, stop_name, stop_lat, stop_lon)
subway_routes <- gtfs_cta$routes %>%
  filter(route_type == 1) %>%
  select(route_id, route_long_name, route_color)
gtfs_sf <- tidytransit::gtfs_as_sf(gtfs_cta, crs = 4326)

trips <- gtfs_cta$trips %>% 
               filter(route_id %in% subway_routes$route_id) %>% 
               select(shape_id, route_id) %>%
  distinct(shape_id, .keep_all = TRUE)

subway_sf <- gtfs_sf$shapes %>%
  inner_join(trips, by = "shape_id") %>%
  left_join(subway_routes, by = "route_id") %>%
  mutate(route_color = paste0("#", route_color))

station_sf <- st_as_sf(subway_stations, coords = c("stop_lon", "stop_lat"), crs = 4326)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap(leaflet::providers$CartoDB.DarkMatter) + 
  tm_shape(subway_sf) + tm_lines(col = "route_color", lwd=2) +
  tm_shape(station_sf) + tm_dots(size = 0.02, border.col = "white", border.lwd = 3)
ridership <- read.csv("CTA_-_Ridership_-__L__Station_Entries_-_Daily_Totals_20240922.csv")
ridership <- ridership %>%
  mutate(station_id = as.character(station_id)) %>%
  mutate(date = trimws(date), date = mdy(date)) %>%
  filter(date >= as.Date("2015-01-01") & date <= as.Date("2019-12-31"))
avg_rides <- ridership %>% 
  filter(daytype == "W") %>%
  mutate(year = year(date)) %>% 
  group_by(station_id, stop_name) %>%
  #group_by(station_id, stop_name, year) %>% 
  summarise(avg_rides = mean(rides, na.rm = TRUE), .groups = 'drop') 
stations_ridership <- station_sf %>%
  left_join(avg_rides, by = c("stop_id" = "station_id"))
tm_basemap(leaflet::providers$CartoDB.Positron) + 
  tm_shape(subway_sf) + tm_lines(col = "route_color", lwd=2) +
  tm_shape(stations_ridership) + tm_dots(col = "pink", size = "avg_rides")
## Legend for symbol sizes not available in view mode.
census_api_key(Sys.getenv("census_api"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
census_data <- get_acs(geography = "block group",
                  state="IL",
                  county=c("Cook", "Lake"),
                  variables = c(hhincome = 'B19013_001', population = 'B01003_001'),
                  survey = "acs5", 
                  geometry = TRUE, 
                  output = "wide",
                  progress = FALSE) %>% 
  rename(hhincome = hhincomeE, population = populationE) %>% 
  st_transform(4326) %>% 
  filter(!st_is_empty(geometry))
## 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)`.
census_data <- census_data %>%
  mutate(pop_den = population / st_area(.) * 1000)
tm_basemap(leaflet::providers$CartoDB.Positron) + 
  tm_shape(census_data) + tm_polygons(col = "pop_den", style = "jenks", popup.vars = c("hhincome"="hhincome", "population"="population"), alpha = 0.5) +
  tm_shape(subway_sf) + tm_lines(col = "red", lwd=2) +
  tm_shape(stations_ridership) + tm_dots(col = "avg_rides", style = "jenks", palette = "Blues") 
## Use "fisher" instead of "jenks" for larger data sets