library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(tidycensus)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tidyr)
Importing the Census API
tidycensus::census_api_key(Sys.getenv("Census_API"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
hospital_data <- st_read("https://raw.githubusercontent.com/ujhwang/urban-analytics-2024/main/Assignment/mini_3/yelp_hospital.geojson")
## Reading layer `yelp_hospital' from data source
## `https://raw.githubusercontent.com/ujhwang/urban-analytics-2024/main/Assignment/mini_3/yelp_hospital.geojson'
## using driver `GeoJSON'
## Simple feature collection with 129 features and 23 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -84.56242 ymin: 33.60009 xmax: -84.08677 ymax: 34.0701
## Geodetic CRS: WGS 84
acs_tract <- suppressMessages(
get_acs(geography = "tract",
state = "GA",
county = c("Fulton", "Dekalb"),
# chose 3 variables: median house hold income, poverty status, and population
# Download ACS data for Fulton and DeKalb counties
acs_vars <- c(median_income = "B19013_001",
percent_poverty = "B17001_002",
percent_uninsured = "B27001_001",
percent_black = "B02009_001"),
year = 2021,
survey = "acs5", # American Community Survey 5-year estimate
geometry = TRUE, # returns sf objects
output = "wide") # wide vs. long
)
## | | | 0% | |= | 1% | |== | 2% | |== | 3% | |== | 4% | |=== | 4% | |=== | 5% | |==== | 5% | |===== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======== | 12% | |========= | 13% | |========== | 14% | |============ | 17% | |============= | 18% | |============== | 20% | |=============== | 22% | |================= | 24% | |====================== | 32% | |============================= | 41% | |================================== | 49% | |====================================== | 54% | |======================================== | 57% | |============================================== | 65% | |================================================= | 70% | |================================================== | 72% | |=================================================== | 73% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 86% | |============================================================= | 87% | |================================================================ | 91% | |================================================================ | 92% | |================================================================== | 94% | |=================================================================== | 95% | |=================================================================== | 96% | |======================================================================| 100%
acs_tract_clean = acs_tract %>%
drop_na()
hospital_data_clean = hospital_data %>%
drop_na()
acs_tract_clean = st_transform(acs_tract_clean, crs = st_crs(hospital_data_clean))
#Joining the datasets
hospital_acs = st_join(acs_tract_clean, hospital_data_clean)
#Create a 0.25 mile buffer (402.336 meters)
acs_tract_buffer <- st_buffer(acs_tract_clean, dist = 402.336)
# Intersect with hospitals
hospitals_within_buffer <- st_intersects(acs_tract_buffer,hospital_data_clean)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(acs_tract_buffer) + # ACS Census tracts with buffer
tm_polygons(col = "red", border.col = "cyan", alpha = 0.5, title = "Census Tract Buffer (0.25 mile)") +
tm_shape(hospital_data) + # Hospital locations
tm_dots(col = "yellow", size = 0.05, title = "Hospitals") +
tm_layout(title = "Hospital Locations with 0.25-mile Buffer", legend.outside = TRUE)