You will see # TASK ///// through out this template. This indicates the beginning of a task. Right below it will be instructions for the task. Each # TASK ///// will be paired with # //TASK ///// to indicate where that specific task ends.
For example, if you see something like below…
# TASK ////////////////////////////////////////////////////////////////////////
# create a vector with element 1,2,3 and assign it to `my_vec` object
# **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
What I expect you to do is to replace where it says
# **YOUR CODE HERE..** with your answer, like below.
# TASK ////////////////////////////////////////////////////////////////////////
# create a vector with element 1,2,3 and assign it to `my_vec` object
my_vec <- c(1,2,3)
# //TASK //////////////////////////////////////////////////////////////////////
Some instructions may involve multiple steps, as shown below. You can use the pipe operator to chain multiple functions together to complete the task. Make sure to assign the output of your code to an object with the specified name. This ensures that your code runs smoothly—if you change the object name (e.g., subset_car in the example below), the subsequent code will not run correctly.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Using mtcars object, extract rows where cyl equals 4
# 2. Select mpg and disp columns
# 3. Create a new column 'summation' by adding mpg and disp
# 4. assign it into `subset_car` object
#subset_car <- # **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
I expect you to replace where it says
# **YOUR CODE HERE..** with your answer, like below.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Using mtcars object, extract rows where cyl equals 4
# 2. Select mpg and disp columns
# 3. Create a new column 'summation' by adding mpg and disp
# 4. assign it into `subset_car` object
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
subset_car <- mtcars %>%
filter(cyl == 4) %>%
select(mpg, disp) %>%
mutate(summation = mpg + disp)
# //TASK //////////////////////////////////////////////////////////////////////
You will need to knit it, publish it on Rpubs, and submit the link.
In this assignment, you will simulate a journey that begins at a Census Tract centroid, drives to the nearest MARTA station, and then take the MARTA to the Midtown station. You will then compare how travel time varies by different Census Tracts. The steps and data required for this analysis are outlined below.
Step 1. Download the required GTFS data. Convert it to sf format, extract MARTA rail stations, and clean the stop names to remove duplicates. Also, extract the destination station.
Step 2. Download the required Census data. Convert Census Tract polygons into centroids and create a subset for analysis.
Step 3. Download the required OSM data. Convert it into an sfnetwork object and clean the network.
Step 4. Simulate a park-and-ride trip (from a test origin → closest station → Midtown station).
Step 5. Turn the simulation process from Step 4 into a reusable function.
Step 6. Apply the function from Step 5 iteratively to all home locations.
Step 7.Finally, create maps and plots to analyze whether there are any disparities in transit accessibility when commuting to Midtown.
Importing the necessary packages is part of this assignment. Add any required packages to the code chunk below as you progress through the tasks.
library(tidytransit )
## Warning: package 'tidytransit' was built under R version 4.3.3
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tibble' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.2 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.3.3
library(dplyr)
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(sfnetworks)
## Warning: package 'sfnetworks' was built under R version 4.3.3
library(tidygraph)
## Warning: package 'tidygraph' was built under R version 4.3.1
##
## Attaching package: 'tidygraph'
##
## The following object is masked from 'package:stats':
##
## filter
library(tmap)
# TASK ////////////////////////////////////////////////////////////////////////
# Download MARTA GTFS data and assign it to `gtfs` object
gtfs <- read_gtfs("https://www.itsmarta.com/google_transit_feed/google_transit.zip")
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Edit stop_name to append serial numbers (1, 2, etc.) to remove duplicate names
stop_dist <- stop_group_distances(gtfs$stops, by='stop_name') %>%
filter(dist_max > 200)
gtfs$stops <- gtfs$stops %>%
group_by(stop_name) %>%
mutate(stop_name = case_when(stop_name %in% stop_dist$stop_name ~ paste0(stop_name, " (", seq(1,n()), ")"),
TRUE ~ stop_name))
# Create a transfer table
gtfs <- gtfsrouter::gtfs_transfer_table(gtfs,
d_limit = 200,
min_transfer_time = 120)
## Registered S3 method overwritten by 'gtfsrouter':
## method from
## summary.gtfs gtfsio
# NOTE: Converting to sf format uses stop_lat and stop_lon columns contained in gtfs$stops.
# In the conversion process, stop_lat and stop_lon are converted into a geometry column, and
# the output sf object do not have the lat lon column anymore.
# But many other functions in tidytransit look for stop_lat and stop_lon.
# So I re-create them using mutate().
gtfs <- gtfs %>% gtfs_as_sf(crs = 4326)
gtfs$stops <- gtfs$stops %>%
ungroup() %>%
mutate(stop_lat = st_coordinates(.)[,2],
stop_lon = st_coordinates(.)[,1])
# Get stop_id for rails and buses
rail_stops <- gtfs$routes %>%
filter(route_type %in% c(1)) %>%
inner_join(gtfs$trips, by = "route_id") %>%
inner_join(gtfs$stop_times, by = "trip_id") %>%
inner_join(gtfs$stops, by = "stop_id") %>%
group_by(stop_id) %>%
slice(1) %>%
pull(stop_id)
# Extract MARTA rail stations
station <- gtfs$stops %>% filter(stop_id %in% rail_stops)
# Extract Midtown Station
midtown <- gtfs$stops %>% filter(stop_id == "134")
# Create a bounding box to which we limit our analysis
bbox <- st_bbox(c(xmin = -84.45241, ymin = 33.72109, xmax = -84.35009, ymax = 33.80101),
crs = st_crs(4326)) %>%
st_as_sfc()
# =========== NO MODIFY ZONE ENDS HERE ========================================
# TASK ////////////////////////////////////////////////////////////////////////
# Specify Census API key whichever you prefer using census_api_key() function
census_api_key(Sys.getenv("CENSUS_API"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Using get_acs() function, download Census Tract level data for 2022 for Fulton, DeKalb, and Clayton in GA.
# and assign it to `census` object.
# Make sure you set geometry = TRUE.
# Required data from the Census ACS:
# 1) Median Household Income (name the column `hhinc`)
# 2) Minority Population (%) (name the column `pct_minority`)
# Note: You may need to download two or more Census ACS variables to calculate minority population (%). "Minority" here can refer to either racial minorities or racial+ethnic minorities -- it's your choice.
census <- get_acs(
geography = "tract",
variables = c(
hhinc = "B19013_001",
total_pop = "B03002_001",
white_nonhisp = "B03002_003"
),
year = 2022,
state = "GA",
county = c("Fulton", "DeKalb", "Clayton"),
geometry = TRUE
) %>%
select(GEOID, variable, estimate, geometry) %>%
spread(variable, estimate) %>%
mutate(
pct_minority = (1 - (white_nonhisp / total_pop)) * 100
) %>%
select(GEOID, hhinc, pct_minority, geometry)
## Getting data from the 2018-2022 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## | | | 0% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 86% | |============================================================= | 86% | |============================================================= | 87% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Convert the CRS of `census` to GCS
census <- census %>%
st_transform(crs = 4326)
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# get centroids of `census` polygons,
# extract centroids that fall inside the `bbox`,
# and assign it to `home` object.
home <- census %>%
st_centroid() %>%
st_intersection(bbox)
## Warning: st_centroid assumes attributes are constant over geometries
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Get OSM road data for all road types for the `bbox` area.
# 3. Convert the OSM data into an sf object
# 4. Convert osmdata polygons into lines
osm_road <- opq(bbox = bbox) %>%
add_osm_feature(key = 'highway',
value = c("motorway", "motorway_link",
"trunk", "trunk_link",
"primary", "primary_link",
"secondary", "secondary_link",
"tertiary", "residential")) %>%
osmdata_sf() %>%
osm_poly2line()
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Convert osm_road$osm_line into sfnetwork
# 2. Activate edges
# 3. Clean the network using the methods we learned
# 4. Assign the cleaned network to an object named 'osm'
osm <- sfnetworks::as_sfnetwork(osm_road$osm_line, directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop())
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Add a new column named 'length' to the edges part of the object `osm`.
osm <- osm %>%
activate("edges") %>%
mutate(length = st_length(geometry))
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Extract the first row from `home` object and store it `home_1`
home_1 <- home[1,]
# =========== NO MODIFY ZONE ENDS HERE ========================================
# TASK ////////////////////////////////////////////////////////////////////////
# Find the shortest paths from `home_1` to all other stations
paths <- st_network_paths(osm, from = home_1)
## Warning in shortest_paths(x, from, to, weights = weights, output = "both", : At
## vendor/cigraph/src/paths/dijkstra.c:534 : Couldn't reach some vertices.
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Get shortest network distances from `home_1` to all other stations.
dist_all <- map_dbl(1:nrow(paths), function(x){
osm %>%
activate("nodes") %>%
slice(paths$node_paths[[x]]) %>%
st_as_sf("edges") %>%
pull(length) %>%
sum()
}) %>% unlist()
# Replace zeros with a large value.
if (any(dist_all == 0)){
dist_all[dist_all == 0] <- max(dist_all)
}
# TASK ////////////////////////////////////////////////////////////////////////
# Find the closest station and assign the value to `closest_station`
closest_station <- st_nearest_feature(home_1, station)
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Get the distance to the closest station.
closest_dist <- st_distance(home_1, station[closest_station, ], by_element = TRUE)
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Calculate travel time based on the `closest_dist` assuming we drive at 20 miles/hour speed.
# Assign the value to `trvt_osm_m` object.
trvt_osm_m <- as.numeric(closest_dist) / 8.94
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Create a subset of stop_times data table for date = 2025-11-10,
# minimum departure time of 7AM, maximum departure time of 10AM.
# Assign the output to `am_stop_time` object
am_stop_time <- filter_stop_times(gtfs_obj = gtfs,
extract_date = "2025-11-10",
min_departure_time = 3600*7,
max_arrival_time = 3600*10)
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Calculate travel times from the `closest_station` to all other stations
# during time specified in `am_stop_time`. Allow ONE transfer.
# 2. Filter the row where the destination is Midtown station.
# 3. Assign it to `trvt` object.
trvt <- travel_times(filtered_stop_times = am_stop_time,
stop_name = "MIDTOWN STATION",
time_range = 3600,
arrival = FALSE,
max_transfers = 1,
return_coords = TRUE)
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Divide the calculated travel time by 60 to convert the unit from seconds to minutes.
trvt_gtfs_m <- trvt$travel_time/60
# Add the travel time from home to the nearest station and
# the travel time from the nearest station to Midtown station
total_trvt <- trvt_osm_m + trvt_gtfs_m
# =========== NO MODIFY ZONE ENDS HERE ========================================
# Function definition (do not modify other parts of the code in this code chunk except for those inside the TASK section)
get_trvt <- function(home, osm, station, midtown){
# TASK ////////////////////////////////////////
# If the code in Step 4 runs fine,
# Replace where it says **YOUR CODE HERE..** below with
# the ENTIRETY of the previous code chunk (i.e., Step 4)
home_1 <- home[1, ]
closest_station <- st_nearest_feature(home_1, station)
closest_dist <- st_distance(home_1, station[closest_station, ], by_element = TRUE)
trvt_osm_m <- as.numeric(closest_dist) / 8.94
am_stop_time <- filter_stop_times(
gtfs_obj = gtfs,
extract_date = "2025-11-10",
min_departure_time = 3600 * 7,
max_arrival_time = 3600 * 10
)
closest_station_name <- station$stop_name[closest_station]
trvt <- travel_times(
filtered_stop_times = am_stop_time,
stop_name = closest_station_name,
time_range = 3600,
arrival = FALSE,
max_transfers = 1,
return_coords = TRUE
) %>%
filter(str_detect(to_stop_name, regex("MIDTOWN", ignore_case = TRUE)))
trvt_gtfs_m <- trvt$travel_time / 60
total_trvt <- trvt_osm_m + trvt_gtfs_m
# //TASK //////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
if (length(total_trvt) == 0) {total_trvt = 0}
return(total_trvt)
# =========== NO MODIFY ZONE ENDS HERE ========================================
}
# Prepare an empty vector
total_trvt <- vector("numeric", nrow(home))
# Apply the function to all Census Tracts
# Fill `total_trvt` object with the calculated time
for (i in 1:nrow(home)){
total_trvt[i] <- get_trvt(home[i,], osm, station, midtown)
}
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
## Warning in total_trvt[i] <- get_trvt(home[i, ], osm, station, midtown): number
## of items to replace is not a multiple of replacement length
# cbind the calculated travel time to `home`
home <- home %>%
cbind(trvt = total_trvt)
Create two maps and two plots by following the instructions in the code chunk below. Write a brief (maximum 200 words) description summarizing your observations from the maps and plots
# TASK ////////////////////////////////////////
# Create an interactive map displaying `census` (polygons) and `home` (points), effectively visualizing household income and travel time to Midtown Station, respectively.
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.1
library(htmltools)
## Warning: package 'htmltools' was built under R version 4.3.3
income_pal <- colorQuantile("YlGnBu", domain = census$hhinc, n = 5)
trvt_pal <- colorQuantile("Reds", domain = home$trvt, n = 5)
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addPolygons(data = census, fillColor = ~income_pal(hhinc), color = "white", weight = 0.3, opacity = 0.6, fillOpacity = 0.7
) %>%
addCircles(data = home, fillColor = ~trvt_pal(trvt), stroke = FALSE, radius = 150,fillOpacity = 0.7
) %>%
addLegend("bottomright", pal = income_pal, values = census$hhinc,
title = "Median HH Income ($)", opacity = 0.7) %>%
addLegend("bottomleft", pal = trvt_pal, values = home$trvt,
title = "Travel Time (min)", opacity = 0.7)
# Create an interactive map displaying `census` (polygons) and `home` (points) effectively visualizing the percentage of minority population and travel time to Midtown Station, respectively.
minority_pal <- colorQuantile("Purples", domain = census$pct_minority, n = 5)
trvt_pal <- colorQuantile("Reds", domain = home$trvt, n = 5)
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addPolygons(data = census, fillColor = ~minority_pal(pct_minority), color = "white", weight = 0.3, opacity = 0.6, fillOpacity = 0.7
) %>%
addCircles(data = home,fillColor = ~trvt_pal(trvt),stroke = FALSE,radius = 150, fillOpacity = 0.7) %>%
addLegend("bottomright", pal = minority_pal, values = census$pct_minority,
title = "% Minority Population", opacity = 0.7) %>%
addLegend("bottomleft", pal = trvt_pal, values = home$trvt,
title = "Travel Time (min)", opacity = 0.7)
# Create a scatter plot with a trend line showing the relationship between household income (x-axis) and travel time to Midtown Station (y-axis).
library(ggplot2)
ggplot(home, aes(x = hhinc, y = trvt)) +
geom_point(alpha = 0.6, color = "steelblue") +
geom_smooth(method = "lm", color = "red", se = TRUE) +
labs(
title = "Relationship Between Household Income and Travel Time to Midtown Station",
x = "Median Household Income ($)",
y = "Travel Time to Midtown (minutes)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 6 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_point()`).
# //TASK //////////////////////////////////////
# Create a scatter plot with a trend line showing the relationship between the percentage of minority population (x-axis) and travel time to Midtown Station (y-axis).
ggplot(home, aes(x = pct_minority, y = trvt)) +
geom_point(alpha = 0.6, color = "darkorchid") +
geom_smooth(method = "lm", color = "red", se = TRUE) +
labs(
title = "Relationship Between Minority Population and Travel Time to Midtown Station",
x = "Percent Minority Population (%)",
y = "Travel Time to Midtown (minutes)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'