knitr::opts_chunk$set(message = FALSE, warning = FALSE, fig.align = "center")

# Load the packages used in your analysis.
library(tidyverse)
library(magrittr)
library(sf)
library(tmap)
library(osmdata)
library(sfnetworks)
library(tidygraph)
library(tidycensus)
library(knitr)
library(tigris)
library(ggpubr)
library(kableExtra)
library(jsonlite)
library(ggrepel)
library(leafsync)

How to Use This Starter File

Complete the blank code chunks, provide explanations where requested, and add code comments where necessary. Keep the existing code chunks and section headings. Use methods from class and the course labs; code needed to preserve your inputs is supplied.

Work through the file in order:

  1. Fill in the data-preparation chunks marked eval=FALSE and run them manually in RStudio.
  2. Run save-inputs to save your sample, then complete image collection and Colab processing in Section 4.
  3. Complete the analysis and knit. Leave eval=FALSE in place: the report loads your saved inputs instead of resampling roads or downloading images again.

Save this Rmd, major2_inputs.rds, and the downloaded seg_output.csv in the same project folder. Use a downloaded_image subfolder for the JPG files. Open an RStudio project in that folder so relative paths work both interactively and when knitting. Do not include private credentials in the document.

Assignment Overview

Select two Census tracts that you expect to differ in walkability. Compare their streetscapes using images randomly sampled from each road network. Then compare the tracts within road classes represented in both samples. Does your interpretation change when you compare similar kinds of roads?

The labels “More walkable” and “Less walkable” describe your initial expectations, not a conclusion established by the model. Image composition captures only some aspects of walkability.

Analysis Settings and Sampling Rules

local_crs <- 32616 # PCS / WGS84 / UTM 16N
sampling_seed <- 2026

1. Select Two Tracts

Download tract geometry and choose one tract you expect to be more walkable and another you expect to be less walkable. Your choices can reflect personal experience, a preliminary map review, or another stated reason.

Create tracts, an sf object with two polygon features and columns GEOID and tract_label. GEOID identifies the tract; tract_label records your initial walkability expectation – use the labels "More walkable" and "Less walkable" throughout the analysis.

# Download the tract geometry and select the two tracts.

# Assign tract labels and transform to local_crs for spatial calculations.

tracts <- tracts(
  state = "GA",
  county = c("Fulton", "DeKalb"),
  year = 2024,
  cb = TRUE
) |> 
  filter(GEOID %in% c("13121001102", "13089022203")) |> 
  select(GEOID) |> 
  mutate(
    tract_label = if_else(GEOID == "13121001102", "More walkable", "Less walkable"))

tracts <- tracts |> 
  st_transform(local_crs)

Briefly explain your choices and what streetscape differences you expect to find (no more than 100 words).

Your explanation:

I selected these two tracts based on my personal experience. The more walkable tract selected is in Midtown Atlanta between 10th and 12th streets and adjacent to Piedmont Park. It is close to my home and where a lot of my friends live. The area is mostly on a grid, and has seen significant investment in walkability, lane reduction, and the addition of bike lanes in the past five years. While there are still some narrow sidewalks and sparse parking lots, I expect to see elements of higher walkability overall.

The less walkable tract I selected is in Decatur, close to the Avondale and Scottsdale city limits. It is where my boyfriend lives. I have spent a lot of time in this area and know from personal experience that it is a highly car-dependent, suburban environment with barely used, narrow sidewalks. I do expect to see more greenery reflected in the visual output as it is a fairly green area.

Overall, I expect to see a significant difference in the results, particularly in terms of sidewalks and building-to-street ratios.

2. Prepare and Sample the Road Networks

For each tract:

  1. Download the selected road classes using the tract’s bounding box and clean the network using the lab methods.
  2. Calculate segment lengths. Keep whole segments at least 91.44 meters long whose midpoints fall inside the tract.
  3. Inspect the number of eligible segments by road class.
  4. Use sampling_seed and a consistent row order to sample up to 100 segments across all eligible road classes. Use all segments if fewer than 100 are eligible.

Combine the two samples into edges, an sf object of LINESTRING features in local_crs. Keep tract_label, GEOID, highway, and length (meters). Assign a unique edge_id to each segment across the combined sample.

# Download and clean the road networks for the two tracts.

#Creating more walkable tract
road_network_more <- tracts |> 
  filter(tract_label == "More walkable")|> 
  st_transform(4326) |> 
  st_bbox() |> 
  opq() |> 
  add_osm_feature(key = "highway", 
                  value = c("primary", "secondary", "tertiary", "residential")) |> 
  osmdata_sf() |> 
  osm_poly2line()
#Creating less walkable tract
road_network_less <- tracts |> 
  filter(tract_label == "Less walkable")|> 
  st_transform(4326) |> 
  st_bbox() |> 
  opq() |> 
  add_osm_feature(key = "highway", 
                  value = c("primary", "secondary", "tertiary", "residential")) |> 
  osmdata_sf() |> 
  osm_poly2line()
# Calculate lengths and retain segments with eligible midpoints and lengths.
roads_more <- road_network_more$osm_lines |> 
  st_transform(local_crs) |> 
  select(osm_id, highway) |> 
  as_sfnetwork(directed = FALSE) |>  
  activate(edges) |> 
  filter(!edge_is_multiple()) |> 
  filter(!edge_is_loop()) |> 
  convert(to_spatial_subdivision) |> 
  convert(to_spatial_smooth, require_equal = "highway", summarise_attributes = "first") |> 
  mutate(length = as.numeric(st_length(geometry)))

roads_less <- road_network_less$osm_lines |> 
  st_transform(local_crs) |> 
  select(osm_id, highway) |> 
  as_sfnetwork(directed = FALSE) |>  
  activate(edges) |> 
  filter(!edge_is_multiple()) |> 
  filter(!edge_is_loop()) |> 
  convert(to_spatial_subdivision) |> 
  convert(to_spatial_smooth, require_equal = "highway", summarise_attributes = "first") |> 
  mutate(length = as.numeric(st_length(geometry)))

segments_more <- roads_more |> 
  st_as_sf() |> 
  mutate(midpoint =
    st_line_sample(geometry, sample = 0.5) |> 
  st_cast("POINT")
  ) |> 
  filter(length >= 91.44) |> 
  st_set_geometry("midpoint") |> 
  st_join(filter(tracts, tract_label == "More walkable"), join = st_within, left = FALSE) |> 
  st_set_geometry("geometry")

segments_less <- roads_less |> 
  st_as_sf() |> 
  mutate(midpoint =
    st_line_sample(geometry, sample = 0.5) |> 
  st_cast("POINT")
  ) |> 
  filter(length >= 91.44) |> 
  st_set_geometry("midpoint") |> 
  st_join(filter(tracts, tract_label == "Less walkable"), join = st_within, left = FALSE) |> 
  st_set_geometry("geometry")

# Combine the samples and assign unique edge IDs.

combined_roads <- bind_rows(segments_more, segments_less) |> 
  select('tract_label', 'GEOID', 'highway', 'length') |> 
  st_transform(local_crs) |> 
  rowid_to_column(var = "edge_id")

3. Prepare the Image Locations and Headings

Adapt getHeading() from the GSV lab to take one projected LINESTRING and return two midpoint views. Sample points at 0.48, 0.50, and 0.52 along the segment. From the midpoint (0.50), calculate headings toward the other two points, looking toward each end of the segment.

Return two rows with type ("mid1" and "mid2"), X, Y, and heading. Use the midpoint coordinates for both rows and express headings from 0 to less than 360 degrees.

getHeading <- function(line) {

  mid_p3 <- line %>% 
    st_line_sample(sample = c(0.48, 0.5, 0.52)) %>% 
    st_cast("POINT") %>% 
    st_coordinates()
  
  mid_p <- mid_p3[2,]
  
  mid_heading_1 <- atan2(mid_p3[1,"X"] - mid_p3[2, "X"],
                     mid_p3[1,"Y"] - mid_p3[2, "Y"])*180/pi
  
  mid_heading_2 <- atan2(mid_p3[3,"X"] - mid_p3[2, "X"],
                     mid_p3[3,"Y"] - mid_p3[2, "Y"])*180/pi
  
  mid_heading_1 <- mid_heading_1 %% 360
  mid_heading_2 <- mid_heading_2 %% 360

  return(tribble(
    ~type,     ~X,             ~Y,             ~heading,
    "mid1",    mid_p["X"],     mid_p["Y"],     mid_heading_1,
    "mid2",    mid_p["X"],     mid_p["Y"],     mid_heading_2))
}

Apply the function to edges and create edges_heading. Create points using local_crs, and transform them to EPSG:4326. Update X and Y to longitude and latitude after transforming. Assign unique img_id values across both tracts. Colab extracts the image ID from the filename and preserves it in the CSV.

edges_heading must have two POINT rows per sampled segment and contain edge_id, img_id, tract_label, GEOID, highway, type, X, Y, and heading.

# Apply getHeading() and attach the segment attributes.
edges_heading <- combined_roads |> 
  st_geometry() |> 
  map_df(getHeading)

# Transform the points, update longitude/latitude, and assign image IDs.
edges_heading <- edges_heading |> 
  bind_cols(
    combined_roads |> 
      st_drop_geometry() |> 
      slice(rep(1:nrow(combined_roads), each=2))
  ) |> 
  rowid_to_column(var = "img_id") |> 
  st_as_sf(coords = c("X", "Y"), crs = local_crs) |> 
  st_transform(4326) |> 
  mutate(
    X = st_coordinates(geometry)[, "X"],
    Y = st_coordinates(geometry)[, "Y"]
  ) |> 
  select('edge_id', 'img_id', 'tract_label', 'GEOID', 'highway', 'type', 'X', 'Y', 'heading')

edges <- combined_roads

edges_heading

NOTE: As both tracts contain less than 100 segments, all segments are used in the analysis.

Run the supplied save chunk manually after preparing your sample. If you change the tracts or sampling before collecting images, regenerate and save the inputs together. Once image collection starts, keep the IDs fixed.

# Provided: run once after preparing your inputs.
saveRDS(
  list(tracts = tracts, edges = edges, edges_heading = edges_heading),
  "major2_inputs.rds"
)
# Provided: restore the same inputs when knitting.
saved_inputs <- readRDS("major2_inputs.rds")
tracts <- saved_inputs$tracts
edges <- saved_inputs$edges
edges_heading <- saved_inputs$edges_heading

stopifnot(nrow(tracts) == 2, !anyDuplicated(tracts$GEOID))
stopifnot(setequal(tracts$tract_label, c("More walkable", "Less walkable")))
stopifnot(!anyNA(edges$edge_id), !anyDuplicated(edges$edge_id))
stopifnot(nrow(edges_heading) == 2 * nrow(edges))
stopifnot(!anyNA(edges_heading$img_id), !anyDuplicated(edges_heading$img_id))
stopifnot(!anyNA(edges_heading$edge_id), !anyNA(edges_heading$type))
stopifnot(setequal(edges_heading$edge_id, edges$edge_id))

# Each sampled segment must have exactly one view of each type.
view_counts <- edges_heading %>%
  sf::st_drop_geometry() %>%
  dplyr::group_by(edge_id) %>%
  dplyr::summarise(
    n_views = dplyr::n(),
    n_mid1 = sum(type == "mid1"),
    n_mid2 = sum(type == "mid2"),
    .groups = "drop"
  )
stopifnot(all(view_counts$n_views == 2L),
          all(view_counts$n_mid1 == 1L),
          all(view_counts$n_mid2 == 1L))

Create an interactive map showing both selected tracts with clearly identified tract labels. Include this map in the knitted report.

# Map the selected tract polygons and identify the two tracts.
tmap_mode("view")

tm_basemap("Esri.WorldGrayCanvas") +
tm_shape(tracts) +
  tm_polygons(
    fill = "tract_label",
    fill.scale = tm_scale_categorical(
      values = c("More walkable" = "#2A9D8F",
                 "Less walkable" = "#E76F51")
    ),
    fill.legend = tm_legend(title = "Walkability Label"),
    col = "black",
    lwd = 2,
    fill_alpha = 0.5,
    popup.vars = c(
      "Tract" = "GEOID",
      "Walkability" = "tract_label"
    )
  ) +
  tm_text(
    text = "tract_label",
    size = 0.8,
    col = "black"
  ) +
  tm_view(bbox = st_bbox(tracts))

4. Download Images and Run Colab

Adapt getImage() from the GSV lab to download one image from one row of edges_heading:

  1. Extract type, img_id, edge_id, and heading. Format location as latitude,longitude (Y,X).
  2. Read your Google API key from its environment variable and construct request using the lab’s Street View Static API endpoint and image settings.
  3. Set fpath to the supplied filename inside downloaded_image.

The filename and download commands are provided below.

getImage <- function(iterrow) {
  # Extract row values, format location and heading, and construct the request URL.
  type = iterrow$type
  location <- paste0(iterrow$Y %>% round(5), ",", iterrow$X %>% round(5))
  heading <- iterrow$heading %>% round(1)
  edge_id <- iterrow$edge_id
  img_id <- iterrow$img_id
  highway <- iterrow$highway
  key <- Sys.getenv("GOOGLE_API_KEY")
  
  endpoint <- "https://maps.googleapis.com/maps/api/streetview"
  
  request <- glue::glue("{endpoint}?size=640x640&location={location}&heading={heading}&fov=90&pitch=0&key={key}")

  # Provided filename: keep img_id as the first number in the filename.
  fname <- glue::glue("GSV-img_{img_id}-eid_{edge_id}-type_{type}.jpg")

  # Create fpath by combining the output folder with fname.
  fpath <- file.path("downloaded_image", fname)

  # Provided: skip an existing file rather than request it again.
  if (!file.exists(fpath)) {
    download.file(request, fpath, mode = "wb", quiet = TRUE)
    Sys.sleep(1)
  }
}

Run the supplied download chunk manually, keeping eval=FALSE. Inspect the images and remove gray “no imagery” placeholders or invalid files before running Colab. Do not change or reuse image IDs to fill gaps. Contact me if technical problems prevent you from completing collection or processing.

# Provided: run manually, not when knitting.
dir.create("downloaded_image", showWarnings = FALSE)
for (i in seq_len(nrow(edges_heading))) {
  getImage(edges_heading[i, ])
}

Zip the image folder as gsv_images.zip. Use the Colab notebook shared for the computer-vision lab (https://colab.research.google.com/drive/1s0HiZIwjke-xtmHwzFmFp6PJrNkysoy9), set the extracted image-folder path, run segmentation on all images, and download seg_output.csv to your project folder. Running the supplied Python is required; writing Python or changing the model is not assessed. Do not include the notebook’s demonstration outputs or individual image predictions in this report.

5. Join Results and Create Segment-Level Measures

Read seg_output.csv and join it to edges_heading using the shared img_id column. Create image_results, with one row per matching image and the original segment and tract attributes.

Determine the total number of pixels in the output mask from Colab or by summing all category counts for one image, as in the lab. Use that total to calculate these six image-level measures. As in the lab, the five pct_* measures are percentages from 0 to 100; multiply the pixel fractions by 100 and label their plots and tables accordingly. The building-to-street ratio is not a percentage:

# Read the CSV, join image IDs to locations, and calculate image-level measures.

seg_output <- read_csv("seg_output.csv")

image_results <- seg_output |> 
  left_join(edges_heading, by = "img_id")

total_pixels <- (768*768)

edges_seg_output <- image_results |> 
  mutate(pct_building = building / total_pixels * 100,
         pct_sky = sky / total_pixels * 100,
         pct_road = road / total_pixels * 100,
         pct_sidewalk = sidewalk / total_pixels * 100,
         pct_greenness = (vegetation + terrain)/total_pixels * 100,
         street_pixels = (road + sidewalk + car),
         b2s_ratio = building / street_pixels,
         b2s_ratio = if_else(street_pixels == 0 | b2s_ratio > 4, NA_real_, b2s_ratio))

Create segment_results, an sf object with one midpoint POINT per segment. Keep only segments with both views (mid1 and mid2) and valid values for all six measures in each view (no missing or infinite values). Keep excluded images in image_results.

Average each image-level measure across the segment’s two views. In particular, average the two image-level building-to-street ratios rather than calculate a new ratio from pooled counts. Keep edge_id, GEOID, tract_label, highway, and the six measure names. Drop geometry before grouping and summarizing measurements, then join the summary back to one midpoint per segment.

# Identify complete pairs with valid measurements.
valid_segments <- edges_seg_output |>
  st_drop_geometry() |>
  group_by(edge_id) |>
  filter(
    n() == 2,
    all(c("mid1", "mid2") %in% type),
    all(if_all(all_of(c(
  "pct_building", "pct_sky", "pct_road","pct_sidewalk", "pct_greenness", "b2s_ratio")), is.finite))
  ) |>
  ungroup()

# Average the two views within each segment and restore one midpoint geometry.
segment_results <- valid_segments |>
  group_by(edge_id) |> 
  summarize(
    GEOID = first(GEOID),
    tract_label = first(tract_label),
    highway = first(highway),
    pct_building = mean(pct_building),
    pct_sky= mean(pct_sky),
    pct_road= mean(pct_road),
    pct_sidewalk= mean(pct_sidewalk),
    pct_greenness= mean(pct_greenness),
    b2s_ratio= mean(b2s_ratio),
    .groups = "drop"
  )

segment_results <- segment_results |> 
  left_join((edges_seg_output |> select(edge_id, geometry)),
                               by = "edge_id", multiple = "first") |> 
  st_as_sf()

6. Compare the Two Tracts Overall

Use segment_results to create:

  1. Interactive maps: Map sidewalk percentage, greenness percentage, and building-to-street ratio. For each measure, show both tracts together or in two separate maps. Use the same color scale for both tracts and include tract boundaries, labels, and legends.
  2. Boxplots: Create one figure with six panels, one per measure. Put tract_label on the x-axis and the measure on the y-axis, giving two boxes per panel. Use readable panel titles. Each panel may have its own y-axis scale, shared by both tracts.
  3. Summary table: Create and display tract_summary with one row per tract. Include the sampled segment count from edges, the included segment count from segment_results, and the mean of each of the six measures for included segments.

If a tract has no valid pairs, report zero included segments and NA means. Show the available results and explain any comparisons you cannot make. If neither tract has valid pairs, skip the measure maps and boxplots. Keep the code chunks and required columns in empty result objects.

# Map the three measures and make the two tracts easy to compare.
street_geom <- segment_results |> 
  st_drop_geometry() |> 
  left_join(edges |> select(edge_id, geometry), by = "edge_id") |> 
  st_as_sf()

tmap_mode("view")

make_map <- function(tract_type, measure, palette, legend_title, show_legend = FALSE) {
  
  tract_bbox <- tracts |>
  filter(tract_label == tract_type) |>
  st_bbox()

  measure_max <- max(segment_results[[measure]], na.rm = TRUE)

  tm_shape(tracts |> filter(tract_label == tract_type)) +
    tm_polygons(
      fill = "#CBD5E1", fill_alpha = 0.3,
      col = "#334155", lwd = 2,
      popup.vars = c("Tract" = "GEOID",
                     "Walkability" = "tract_label")
    ) +
    tm_text(
      text = "tract_label",
      size = 0.7,
      col = "#334155"
    ) +
    tm_shape(street_geom |> filter(tract_label == tract_type)) +
    tm_lines(col = "white", lwd = 4) +
    tm_lines(
      col = measure,
      lwd = 3,
      col.scale = tm_scale_continuous(
        values = palette,
        limits = c(0, measure_max)
      ),
      col.legend = tm_legend_hide(),
      popup.vars = setNames(
        c("edge_id", "highway", measure),
        c("Edge ID", "Road Class", legend_title)
      )
    ) +
    tm_shape(segment_results |> filter(tract_label == tract_type)) +
    tm_dots(
      fill = measure,
      size = 0.5,
      col = "black",
      lwd = 1,
      fill.scale = tm_scale_continuous(
        values = palette,
        limits = c(0, measure_max)
      ),
      fill.legend = if (show_legend) {
        tm_legend(title = legend_title)
      } else {
        tm_legend_hide()
      },
      popup.vars = setNames(
        c("edge_id", "highway", measure),
        c("Edge ID", "Road Class", legend_title)
      )
    ) +
    tm_basemap("OpenStreetMap.HOT") +
    tm_view(bbox = tract_bbox)
}

# Row 1: Sidewalk percentage
map_sidewalk_more <- make_map(
  "More walkable", "pct_sidewalk", "Blues", "Sidewalk (%)"
)

map_sidewalk_less <- make_map(
  "Less walkable", "pct_sidewalk", "Blues", "Sidewalk (%)", TRUE
)

# Row 2: Greenness percentage
map_green_more <- make_map(
  "More walkable", "pct_greenness", "Greens", "Greenness (%)"
)

map_green_less <- make_map(
  "Less walkable", "pct_greenness", "Greens", "Greenness (%)", TRUE
)

# Row 3: Building-to-street ratio
map_b2s_more <- make_map(
  "More walkable", "b2s_ratio", "Purples", "Building-to-Street Ratio"
)

map_b2s_less <- make_map(
  "Less walkable", "b2s_ratio", "Purples", "Building-to-Street Ratio", TRUE
)

latticeView(
  tmap_leaflet(map_sidewalk_more),
  tmap_leaflet(map_sidewalk_less),
  tmap_leaflet(map_green_more),
  tmap_leaflet(map_green_less),
  tmap_leaflet(map_b2s_more),
  tmap_leaflet(map_b2s_less),
  ncol = 2,
  sync = list(c(1, 3, 5), c(2, 4, 6)),
  no.initial.sync = TRUE
)
# Compare all six segment-level measures between tracts.
boxplot_data <- segment_results |>
  st_drop_geometry() |>
  select(tract_label, pct_building, pct_sky, pct_road,
         pct_sidewalk, pct_greenness, b2s_ratio) |>
  pivot_longer(
    cols = -tract_label,
    names_to = "measure",
    values_to = "value"
  ) |>
  mutate(
    measure = factor(
      measure,
      levels = c("pct_building", "pct_sky", "pct_road",
                 "pct_sidewalk", "pct_greenness", "b2s_ratio"),
      labels = c("Building (%)", "Sky (%)", "Road (%)",
                 "Sidewalk (%)", "Greenness (%)",
                 "Building-to-Street Ratio")
    )
  )

ggplot(boxplot_data, aes(x = tract_label, y = value, fill = tract_label)) +
  geom_boxplot(width = 0.6, alpha = 0.8, outlier.alpha = 0.6) +
  facet_wrap(~ measure, scales = "free_y", ncol = 2) +
  scale_fill_manual(
    values = c("More walkable" = "#2A9D8F",
               "Less walkable" = "#E76F51")
  ) +
  labs(
    title = "Streetscape Characteristics by Tract",
    subtitle = "Distribution of segment-level measures",
    x = NULL,
    y = NULL
  ) +
  theme_minimal(base_size = 12) +
  theme(
    legend.position = "none",
    strip.text = element_text(face = "bold", size = 11),
    axis.text.x = element_text(size = 9),
    panel.grid.major.x = element_blank(),
    plot.title = element_text(face = "bold")
  )

# Display sampled and included segment counts and mean measures by tract.
# Calculate sampled segment counts.
sample_counts <- edges |>
  st_drop_geometry() |>
  count(GEOID, tract_label, name = "sampled_segments")

# Calculate included segment counts and mean measures.
included_summary <- segment_results |>
  st_drop_geometry() |>
  group_by(GEOID, tract_label) |>
  summarise(
    included_segments = n(),
    across(
      c(pct_building, pct_sky, pct_road,
        pct_sidewalk, pct_greenness, b2s_ratio),
      mean,
      .names = "mean_{.col}"
    ),
    .groups = "drop"
  )

# Combine into one summary table.
tract_summary <- sample_counts |>
  left_join(included_summary, by = c("GEOID", "tract_label")) |>
  mutate(included_segments = replace_na(included_segments, 0L))

# Display the table.
tract_summary |>
  kable(
    digits = 2,
    caption = "Summary of Streetscape Measures by Tract",
    col.names = c(
      "GEOID", "Tract", "Sampled", "Included",
      "Building (%)", "Sky (%)", "Road (%)",
      "Sidewalk (%)", "Greenness (%)", "Building-to-Street Ratio"
    )
  ) |>
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )
Summary of Streetscape Measures by Tract
GEOID Tract Sampled Included Building (%) Sky (%) Road (%) Sidewalk (%) Greenness (%) Building-to-Street Ratio
13089022203 Less walkable 73 72 3.09 23.23 34.94 1.77 34.26 0.09
13121001102 More walkable 20 20 16.79 11.15 35.91 3.55 26.58 0.40

7. Examine Road Composition and Compare Shared Classes

Create and display road_composition from edges, using the original sample before exclusions. For each tract and road class, report the segment count and its proportion of that tract’s sampled segments. Note any of the four selected road classes missing from a tract’s sample; rows for absent classes are optional.

Using segment_results, keep road classes represented in both tracts. Create one figure with six panels, one for each measure. In each panel, put road class on the x-axis and the measure on the y-axis. For each road class, show side-by-side boxplots for “More walkable” and “Less walkable,” using different colors for tract_label. Each panel may use its own y-axis scale, but both tracts must share the same scale within that panel.

Also report the number of included segments by tract and road class in a table. Note any comparisons based on very few segments. If no road classes are shared, explain this and skip the figure.

# Summarize sampled road-class counts and proportions within each tract.
road_composition <- edges |> 
  st_drop_geometry() |> 
  group_by(tract_label, highway) |> 
  summarise("Count" = n()) |> 
  mutate("Percent" = round(Count * 100 / sum(Count), 2)) |> 
  ungroup()

road_composition |>
  arrange(tract_label, highway) |>
  mutate(Percent = sprintf("%.2f%%", Percent)) |>
  kable(
    format = "html",
    col.names = c("Tract", "Road Class", "Segments", "Share"),
    caption = "Composition of Sampled Road Segments",
    align = c("l", "l", "r", "r"),
    escape = FALSE
  ) |>
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE,
    position = "center"
  ) |>
  collapse_rows(
    columns = 1,
    valign = "middle"
  )
Composition of Sampled Road Segments
Tract Road Class Segments Share
Less walkable residential 47 64.38%
secondary 21 28.77%
tertiary 5 6.85%
More walkable residential 11 55.00%
secondary 9 45.00%
# Identify shared classes in segment_results and report their included counts.
road_composition |>
  filter(highway %in% c("residential", "secondary")) |>
  left_join(
    segment_results |>
      st_drop_geometry() |>
      count(highway, tract_label, name = "Included"),
    by = c("highway", "tract_label")
  ) |>
  mutate(
    Included = replace_na(Included, 0L),
    Percent = sprintf("%.2f%%", Percent)
  ) |>
  arrange(highway, tract_label) |>
  select(highway, tract_label, Count, Included, Percent) |>
  kable(
    format = "html",
    col.names = c("Road Class", "Tract", "Sampled", "Included", "Share"),
    caption = "Shared Road Classes Comparison",
    align = c("l", "l", "r", "r", "r")
  ) |>
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE,
    position = "center"
  ) |>
  collapse_rows(
    columns = 1,
    valign = "middle"
  )
Shared Road Classes Comparison
Road Class Tract Sampled Included Share
residential Less walkable 47 46 64.38%
More walkable 11 11 55.00%
secondary Less walkable 21 21 28.77%
More walkable 9 9 45.00%
# Compare the tracts within shared road classes for all six measures.
boxplot_data <- segment_results |>
  filter(highway %in% c("residential", "secondary")) |>
  st_drop_geometry() |>
  select(tract_label, highway, pct_building, pct_sky, pct_road,
         pct_sidewalk, pct_greenness, b2s_ratio) |>
  pivot_longer(
    cols = -c(tract_label, highway),
    names_to = "measure",
    values_to = "value"
  ) |>
  mutate(
    measure = factor(
      measure,
      levels = c("pct_building", "pct_sky", "pct_road",
                 "pct_sidewalk", "pct_greenness", "b2s_ratio"),
      labels = c("Building (%)", "Sky (%)", "Road (%)",
                 "Sidewalk (%)", "Greenness (%)",
                 "Building-to-Street Ratio")
    )
  )

ggplot(boxplot_data, aes(x = highway, y = value, fill = tract_label)) +
  geom_boxplot(
    width = 0.55,
    alpha = 0.85,
    outlier.alpha = 0.5,
    outlier.size = 1.5,
    linewidth = 0.4
  ) +
  facet_wrap(~ measure, scales = "free_y", ncol = 2) +
  scale_x_discrete(
    limits = c("residential", "secondary"),
    labels = c("Residential", "Secondary")
  ) +
  scale_fill_manual(
    values = c("More walkable" = "#2A9D8F",
               "Less walkable" = "#E76F51")
  ) +
  labs(
    title = "Streetscape Characteristics by Road Class",
    x = NULL,
    y = NULL,
    fill = "Tract Label:",
    caption = "Note: Residential and secondary plots for the more walkable tract only based on 11 and 9 samples respectively."
  ) +
  theme_minimal(base_size = 12) +
  theme(
    legend.position = "bottom",
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 10),
    strip.text = element_text(face = "bold", size = 11),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 9),
    panel.grid.major.x = element_blank(),
    panel.grid.minor = element_blank(),
    panel.spacing = unit(1.2, "lines"),
    plot.title = element_text(face = "bold", size = 15),
    plot.caption = element_text(size = 10, colour = "#64748B", hjust = 0.5)
  )

Findings

In no more than 200 words, answer:

  • Do the overall results support your initial walkability expectations?
  • How does the mix of sampled road classes differ between the tracts?
  • Does comparing the tracts within the same road classes change your interpretation?

If a comparison is unavailable, explain why and what you can conclude from the available results.

Your findings:

Overall, the findings support my expectations, but are limited by the small sample size of my data. The more walkable tract has greater average sidewalk coverage (3.55% vs. 1.77%), substantially higher building-to-street ratios (0.40 vs. 0.09), and greater building coverage (16.79% vs. 3.09%). The denser and more pedestrian-friendly environment is reflected by these results. The less walkable tract has greater greenness (34.26% vs. 26.58%) and sky visibility (23.23% vs. 11.15%), which makes sense as this is a more sparse, suburban community with mostly detached single-family homes.

Residential roads account for 64.38% of segments in the less walkable tract, compared with 55% in the more walkable tract. Secondary roads represent 28.77% and 45%, respectively. Only the less walkable tract contains sampled tertiary roads (6.85%). This also makes sense, as the suburban environment is more likely to contain cul-de-sacs and deeper road networks, whereas the grid-like layout of midtown uses fewer streets. Neither tract contains primary roads.

Comparing shared road classes further strengthens the overall findings. The more walkable tract consistently exhibits higher sidewalk coverage, building coverage, and building-to-street ratios within both residential and secondary roads. Greenness differences are significant on residential roads but smaller on secondary roads.

One thing I did not realize before beginning the study was that the Emory Hospital Decatur is included in the less walkable tract’s area. The maps show higher greenery and sidewalk percentage in this area. The inclusion of the hospital and its surrounding streets may influence the tract-level results, since this area differs signficantly in land use and streetscape characteristics from nearby residential streets.

The analysis is limited by sample size, and should also be interpreted with caution due to the unequal sample sizes (72 versus 20 valid segments).