Lab 2: Spatial Analysis and Visualization

Healthcare Access and Equity in Pennsylvania

Author

Liz Crouse

Published

February 13, 2026

Assignment Overview

Learning Objectives: - Apply spatial operations to answer policy-relevant research questions - Integrate census demographic data with spatial analysis - Create publication-quality visualizations and maps - Work with spatial data from multiple sources - Communicate findings effectively for policy audiences


Part 1: Healthcare Access for Vulnerable Populations

Research Question

Which Pennsylvania counties have the highest proportion of vulnerable populations (elderly + low-income) living far from hospitals?

Your analysis should identify counties that should be priorities for healthcare investment and policy intervention.

Required Analysis Steps

Complete the following analysis, documenting each step with code and brief explanations:

Step 1: Data Collection (5 points)

Load the required spatial data: - Pennsylvania county boundaries - Pennsylvania hospitals (from lecture data) - Pennsylvania census tracts

Your Task:

echo = FALSE

# Load required packages
library(tidycensus)
library(tidyverse)
library(stringr)
library(ggplot2)
library(dplyr)
library(tigris)
library(sf)
library(viridis)
library(RColorBrewer)
library(knitr)
library(scales)


# Load spatial data
pa_hospitals <- st_read("C:/Users/lizmc/Desktop/Y2S2/ppa/crouse/labs/lab_2/data/hospitals.geojson")
Reading layer `hospitals' from data source 
  `C:\Users\lizmc\Desktop\Y2S2\ppa\crouse\labs\lab_2\data\hospitals.geojson' 
  using driver `GeoJSON'
Simple feature collection with 223 features and 11 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -80.49621 ymin: 39.75163 xmax: -74.86704 ymax: 42.13403
Geodetic CRS:  WGS 84
pa_counties <- st_read("C:/Users/lizmc/Desktop/Y2S2/ppa/crouse/labs/lab_2/data/Pennsylvania_County_Boundaries")
Reading layer `Pennsylvania_County_Boundaries' from data source 
  `C:\Users\lizmc\Desktop\Y2S2\ppa\crouse\labs\lab_2\data\Pennsylvania_County_Boundaries' 
  using driver `ESRI Shapefile'
Simple feature collection with 67 features and 19 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -8963377 ymin: 4825316 xmax: -8314404 ymax: 5201413
Projected CRS: WGS 84 / Pseudo-Mercator
pa_tracts <- tracts("PA")

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
# Check that all data loaded correctly
plot(pa_hospitals)

plot(pa_counties)

plot(pa_tracts)

st_crs(pa_hospitals)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(pa_counties)
Coordinate Reference System:
  User input: WGS 84 / Pseudo-Mercator 
  wkt:
PROJCRS["WGS 84 / Pseudo-Mercator",
    BASEGEOGCRS["WGS 84",
        ENSEMBLE["World Geodetic System 1984 ensemble",
            MEMBER["World Geodetic System 1984 (Transit)"],
            MEMBER["World Geodetic System 1984 (G730)"],
            MEMBER["World Geodetic System 1984 (G873)"],
            MEMBER["World Geodetic System 1984 (G1150)"],
            MEMBER["World Geodetic System 1984 (G1674)"],
            MEMBER["World Geodetic System 1984 (G1762)"],
            MEMBER["World Geodetic System 1984 (G2139)"],
            MEMBER["World Geodetic System 1984 (G2296)"],
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ENSEMBLEACCURACY[2.0]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4326]],
    CONVERSION["Popular Visualisation Pseudo-Mercator",
        METHOD["Popular Visualisation Pseudo Mercator",
            ID["EPSG",1024]],
        PARAMETER["Latitude of natural origin",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["False easting",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["easting (X)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["northing (Y)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Web mapping and visualisation."],
        AREA["World between 85.06°S and 85.06°N."],
        BBOX[-85.06,-180,85.06,180]],
    ID["EPSG",3857]]
st_crs(pa_tracts)
Coordinate Reference System:
  User input: NAD83 
  wkt:
GEOGCRS["NAD83",
    DATUM["North American Datum 1983",
        ELLIPSOID["GRS 1980",6378137,298.257222101,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4269]]

Questions to answer: - How many hospitals are in your dataset? 223 - How many census tracts? 3,446 - What coordinate reference system is each dataset in? Hospitals and counties are in WGS 84; tracts are in NAD83.


Step 2: Get Demographic Data

Use tidycensus to download tract-level demographic data for Pennsylvania.

Required variables: - Total population - Median household income - Population 65 years and over (you may need to sum multiple age categories)

Your Task:

# Get demographic data from ACS
variables <- c(mhi = "B19013_001",
               pop = "B01003_001",
               m65_66 = "B01001_020",
    m67_69 = "B01001_021",
    m70_74 = "B01001_022",
    m75_79 = "B01001_023",
    m80_84 = "B01001_024",
    m85p   = "B01001_025",

    f65_66 = "B01001_044",
    f67_69 = "B01001_045",
    f70_74 = "B01001_046",
    f75_79 = "B01001_047",
    f80_84 = "B01001_048",
    f85p   = "B01001_049"
  )


tract_demographics <- get_acs(
  geography = "tract",
  state = "PA",
  variables = variables,
  year = 2022,
  survey = "acs5",
  output = "wide"
  
)
tract_demographics <- tract_demographics %>%
  mutate(
    pop_65plus =
      (m65_66E + m67_69E + m70_74E + m75_79E + m80_84E + m85pE +
      f65_66E + f67_69E + f70_74E + f75_79E + f80_84E + f85pE),

    pct_65plus = (pop_65plus / popE) * 100
  )


# Join to tract boundaries
tract_demographics <- left_join(tract_demographics, pa_tracts, by = "GEOID")

#Initial calculations
sum(is.na(tract_demographics$mhiE))
[1] 63
mean(tract_demographics$mhiE, na.rm = TRUE)
[1] 77527.23

Questions to answer: - What year of ACS data are you using? 5-Year ACS 2022 - How many tracts have missing income data? 63 - What is the median income across all PA census tracts? $77,527.23*


Step 3: Define Vulnerable Populations

Identify census tracts with vulnerable populations based on TWO criteria: 1. Low median household income (choose an appropriate threshold) 2. Significant elderly population (choose an appropriate threshold)

Your Task:

# Filter for vulnerable tracts based on your criteria
vulnerable_tracts <- tract_demographics %>%
  filter(mhiE < 64300 & pct_65plus > 25)
old_tracts <- tract_demographics %>%
  filter(pct_65plus > 25)
lowinc_tracts <- tract_demographics %>%
  filter(mhiE < 64300)

Questions to answer: - What income threshold did you choose and why? I chose $64,300 as my low-income threshold because it is 200% of the PA poverty line for a household of four. 200% poverty line is often used as a metric for eligibility for social services, which would likely coincide with the tracts that need additional health services. This represents about 40% of tracts. - What elderly population threshold did you choose and why? I chose 25% because that means at least one in four individuals in the tract are 65+, meaning extra health services may be needed. - How many tracts meet your vulnerability criteria? 230 - What percentage of PA census tracts are considered vulnerable by your definition? 6.7%


Step 4: Calculate Distance to Hospitals

For each vulnerable tract, calculate the distance to the nearest hospital.

Your Task:

# Transform to appropriate projected CRS
st_geometry(vulnerable_tracts) <- "geometry"
vulnerable_tracts <- vulnerable_tracts %>%
  st_transform(crs = 2272)
pa_hospitals <- pa_hospitals %>%
  st_transform(crs = 2272)
# Calculate distance from each tract centroid to nearest hospital
tract_centroids <- st_centroid(vulnerable_tracts)
vulnerable_tracts <- vulnerable_tracts %>%
  mutate(
    nearest_hosp = st_distance(
      tract_centroids, pa_hospitals$geometry[st_nearest_feature(tract_centroids, pa_hospitals)],
      by_element = TRUE))

vulnerable_tracts <- vulnerable_tracts %>%
  mutate(
    nearest_hosp_miles = as.numeric(nearest_hosp) / 1609.34)

mean(vulnerable_tracts$nearest_hosp_miles, na.rm = TRUE)
[1] 18.62521
tracts_15p <- vulnerable_tracts %>%
  filter(nearest_hosp_miles > 15)

Requirements: - Use an appropriate projected coordinate system for Pennsylvania - Calculate distances in miles - Explain why you chose your projection **I used EPSG 2272 because it is a project coordinate system specific to the southern part of Pennsylvania, meaning it will provide more accurate measurements.

Questions to answer: - What is the average distance to the nearest hospital for vulnerable tracts? 18.64 mi - What is the maximum distance? 95.66 mi - How many vulnerable tracts are more than 15 miles from the nearest hospital? 90


Step 5: Identify Underserved Areas

Define “underserved” as vulnerable tracts that are more than 15 miles from the nearest hospital.

Your Task:

# Create underserved variable
vulnerable_tracts <- vulnerable_tracts %>%
  mutate(service_level = case_when(nearest_hosp_miles > 15 ~ "underserved",
            TRUE ~ "served"))
vulnerable_tracts %>%
  count(service_level)
Simple feature collection with 2 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 1204213 ymin: 144992.2 xmax: 2806953 ymax: 1044690
Projected CRS: NAD83 / Pennsylvania South (ftUS)
# A tibble: 2 × 3
  service_level     n                                                   geometry
* <chr>         <int>                            <MULTIPOLYGON [US_survey_foot]>
1 served          139 (((1364680 306535, 1364760 306588.4, 1364926 306701.3, 13…
2 underserved      91 (((1468789 242704.4, 1468958 243992.2, 1469205 246386.1, …

Questions to answer: - How many tracts are underserved? 90 - What percentage of vulnerable tracts are underserved? 39% - Does this surprise you? Why or why not? This does not surprise me. Lower income tracts with high elderly populations are often more rural and therefore may have less access to services. If anything, I am surprised it is not a larger proportion.


Step 6: Aggregate to County Level

Use spatial joins and aggregation to calculate county-level statistics about vulnerable populations and hospital access.

Your Task:

# Spatial join tracts to counties
pa_counties <- st_transform(pa_counties, st_crs(vulnerable_tracts))
county_statistics <- st_join(vulnerable_tracts, pa_counties["COUNTY_NAM"])

# Aggregate statistics by county

county_statistics_summary <- county_statistics %>%
  group_by(COUNTY_NAM) %>%
  summarise(
    n_vuln_tracts = n(),
    n_unders_tracts = sum(service_level == "underserved", na.rm = TRUE),
    underserved_pct = (n_unders_tracts / n_vuln_tracts) * 100,
    avg_dist_nearest_hosp = mean(nearest_hosp_miles, na.rm = TRUE),
    total_pop_vuln = sum(popE, na.rm = TRUE))

underserved_counties <- county_statistics_summary %>%
  filter(underserved_pct == 100)


countygeo_stat_summary <- pa_counties %>%
  st_join(county_statistics_summary)

Required county-level statistics: - Number of vulnerable tracts - Number of underserved tracts
- Percentage of vulnerable tracts that are underserved - Average distance to nearest hospital for vulnerable tracts - Total vulnerable population

Questions to answer: - Which 5 counties have the highest percentage of underserved vulnerable tracts? By my parameters for under-service, there are 17 counties in PA that are entirely underserved. The five with the least service (furthest average distance to nearest hospital) are Dauphin, Perry, Sullivan, Bradford, and Pike Counties. - Which counties have the most vulnerable people living far from hospitals? Counties that are 100% underserved with populations greater than 10,000 are Wayne, McKean, Pike, Tioga, and Clarion Counties. - Are there any patterns in where underserved counties are located? The largest concentration of the most severely underserved tracts is in north central PA, a rural area distant from PA’s two major population centers of Pittsburgh and Philadelphia.


Step 7: Create Summary Table

Create a professional table showing the top 10 priority counties for healthcare investment.

Your Task:

# Create and format priority counties table


underserved_counties_nogeom <- underserved_counties %>%
  st_drop_geometry() %>%
  arrange(desc(avg_dist_nearest_hosp)) %>%
  slice(1:10)
  
underserved_table <- underserved_counties_nogeom %>%
   mutate(
    n_vuln_tracts = comma(n_vuln_tracts),
    n_unders_tracts = comma(n_unders_tracts),
    total_pop_vuln = comma(total_pop_vuln),
    underserved_pct = percent(underserved_pct / 100, accuracy = 0.1),
    avg_dist_nearest_hosp = round(avg_dist_nearest_hosp, 1)) %>%
  kable(col.names = c("County","# Vulnerable Tracts","# Underserved Tracts","% Tracts Underserved","Average Distance from Tract to Nearest Hospital", "Total Vulnerable Population"), caption = "Counties with high populations of elderly and low income individuals with inequitable access to hospitals")

Requirements: - Use knitr::kable() or similar for formatting - Include descriptive column names - Format numbers appropriately (commas for population, percentages, etc.) - Add an informative caption - Sort by priority (you decide the metric)


Part 2: Comprehensive Visualization

Using the skills from Week 3 (Data Visualization), create publication-quality maps and charts.

Map 1: County-Level Choropleth

Create a choropleth map showing healthcare access challenges at the county level.

Your Task:

# Create county-level access map

ggplot(countygeo_stat_summary) + 
  geom_sf(aes(fill = underserved_pct), color = "black") +
  scale_fill_viridis_b(
    option = "mako",
    direction = -1,
    name = 
      "% Underserved") +
  theme_void() + 
  labs(title = "Percentage of Underserved Tracts by County")


Map 2: Detailed Vulnerability Map

Create a map highlighting underserved vulnerable tracts.

Your Task:

# Create detailed tract-level map
pa_tracts <- pa_tracts %>%
  st_transform(2272)

tracts_service_level <- pa_tracts %>%
  st_join(vulnerable_tracts["service_level"], join = st_within)
tracts_service_level <- tracts_service_level %>%
  mutate(service_level = ifelse(is.na(service_level), "served", service_level))
tracts_service_level %>% 
  count(service_level)
Simple feature collection with 2 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 1189586 ymin: 140905.3 xmax: 2814854 ymax: 1165942
Projected CRS: NAD83 / Pennsylvania South (ftUS)
  service_level    n                       geometry
1        served 3355 MULTIPOLYGON (((1318298 149...
2   underserved   91 MULTIPOLYGON (((1468789 242...
ggplot() + 
  geom_sf(
    data = tracts_service_level,
    aes(fill = service_level),
    color = NA) +
  geom_sf(
    data = pa_counties,
    fill = NA,
    color = "black",
    size = 0.2) +
  scale_fill_manual(
    values = c(
      "underserved" = "red",
      "served" = NA),
    na.value = NA) +
  theme_void() +
  labs(
    title = "Vulnerable & Underserved Tracts in PA Counties", subtitle = "Tracts with low median incomes, high 65+ population, and >15 miles to nearest hospital",
    fill = "Service Level")

Chart: Distribution Analysis

Create a visualization showing the distribution of distances to hospitals for vulnerable populations.

Your Task:

# Create distribution visualization
ggplot(data = vulnerable_tracts, aes(x = nearest_hosp_miles)) + 
  geom_histogram(binwidth = 5, color = "black") +
    scale_x_continuous(
    limits = c(0, NA),
    expand = c(0, 0)
  ) +
  scale_y_continuous(
    limits = c(0, NA),
    expand = c(0, 0)) +
     labs(title = "Distribution of distances to nearest hospital among vulnerable PA census tracts",
     x = "Distance to nearest hospital (mi)",
     y = "Number of tracts",
     caption = "The nearest hospital for over 90 vulnerable tracts is more than 15 miles away.")

Suggested chart types: - Histogram or density plot of distances - Box plot comparing distances across regions - Bar chart of underserved tracts by county - Scatter plot of distance vs. vulnerable population size

Requirements: - Clear axes labels with units - Appropriate title - Professional formatting - Brief interpretation (1-2 sentences as a caption or in text)


Part 3: Bring Your Own Data Analysis

# Load your additional dataset

phl <- counties(state = "PA") 

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
phl <- phl %>%
  filter(NAMELSAD == "Philadelphia County")
phl <- phl %>% st_transform(2272)

crime_phl <- st_read("C:/Users/lizmc/Desktop/Y2S2/ppa/crouse/labs/lab_2/data/incidents_part1_part2(1)")
Reading layer `incidents_part1_part2' from data source 
  `C:\Users\lizmc\Desktop\Y2S2\ppa\crouse\labs\lab_2\data\incidents_part1_part2(1)' 
  using driver `ESRI Shapefile'
replacing null geometries with empty geometries
Simple feature collection with 152556 features and 13 fields (with 3718 geometries empty)
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -75.27421 ymin: 5.684342e-14 xmax: 5.684342e-14 ymax: 40.13718
Geodetic CRS:  WGS 84
crime_phl <- crime_phl %>% st_transform(2272)
crime_phl <- st_filter(crime_phl, phl)
                     
schools_phl <- st_read("C:/Users/lizmc/Desktop/Y2S2/ppa/crouse/labs/lab_2/data/Schools")
Reading layer `Schools' from data source 
  `C:\Users\lizmc\Desktop\Y2S2\ppa\crouse\labs\lab_2\data\Schools' 
  using driver `ESRI Shapefile'
Simple feature collection with 490 features and 14 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -8378628 ymin: 4852555 xmax: -8345686 ymax: 4884813
Projected CRS: WGS 84 / Pseudo-Mercator
schools_phl <- schools_phl %>% st_transform(2272)

Questions to answer: - What dataset did you choose and why? I chose to look at crimes in school zones to identify which schools might be at the highest safety risk, especially during commutes. - What is the data source and date? Bike network: Department of Streets via OpenDataPhilly, last updated October 2025 Schools: Department of Planning and Development via OpenDataPhilly, last updated January 2026 - How many features does it contain? Schools: 490 Crime:148,812 - What CRS is it in? Did you need to transform it? Both were in WGS 84, transformed to PA South 2272


  1. Pose a research question Which school zones are surrounded by the most (reported) crime?

  1. Conduct spatial analysis

Use at least TWO spatial operations to answer your research question.

Required operations (choose 2+): - Buffers - Spatial joins - Spatial filtering with predicates - Distance calculations - Intersections or unions - Point-in-polygon aggregation

#Clean dataset by eliminating duplicates

schools_phl <- schools_phl %>%
  filter(!is.na(school_nam), school_nam != "")
schools_unique <- schools_phl %>%
  group_by(school_nam) %>%
  summarise(
    geometry = st_union(geometry),
    .groups = "drop")

#Create buffers
school_buffers <- st_buffer(schools_unique, dist = 1000)

#Join with crime data
crime_in_school_zones <- st_join(
  crime_phl,
  school_buffers,
  join = st_within,
  left = FALSE)

#Count number of crimes in buffer zones (point in polygon)
crime_by_school <- crime_in_school_zones %>%
  group_by(school_nam) %>%  
  summarise(
    n_crimes = n(),
    .groups = "drop")

#Spatial join back to buffers
school_buffers_crime <- school_buffers %>%
  left_join(
    st_drop_geometry(crime_by_school),
    by = "school_nam") %>%
  mutate(
    n_crimes = replace_na(n_crimes, 0))

#Plot density of crimes in school buffer zones
ggplot() +
  geom_sf(data = school_buffers_crime,
          aes(fill = n_crimes),
          color = "NA",
          alpha = 0.7) +
    geom_sf(data = phl,
    fill = NA,
    color = "black",
    linewidth = 0.8) +
  scale_fill_viridis_c(name = "# Crimes in 1000ft") +
  theme_void() +
  labs(title = "School Safety Zones (1000 ft) and Crime Incidents")

#Identify school zones with the highest number of crimes for potential interventions
highest_risk_schools <- school_buffers_crime %>%
  st_drop_geometry() %>%
  slice_max(n_crimes, n= 10)

highest_risk_table <- highest_risk_schools %>%
  kable(col.names = c("School Name", "# of reported crimes in 1000 ft radius (2025)"), caption = "Schools with the most reported crimes within 1000 foot radius, 2025")

Analysis requirements: - Clear code comments explaining each step - Appropriate CRS transformations - Summary statistics or counts - At least one map showing your findings - Brief interpretation of results (3-5 sentences)

Your interpretation:

An analysis of reported crimes in 2025 within 1,000 feet of schools shows clear clustering of high-crime school zones on the eastern side of the city. First, a cluster appears in the Kensington area, showing the highest number of crimes. Then, another cluster appears in North/Northeast Philadelphia. However, of the top ten schools with the most crime in 1,000 feet, about half are in the Center City area. This reveals an obstacle in the data: areas with more density experience more crime because there is more activity. While this can still provide telling information about school safety, it is important to consider the context of population density and how it may contribute to designations of schools as “safe” or “unsafe.”