knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

options(scipen = 999)

opmitools

opmitools serves 3 primary purposes:

  1. Functions for cleaning and merging GTFS files (building on tidytransit)
  2. Presenting data consistent with OPMI's Office Theme (building on ggplot2)
    1. This part of the package draws on "Creating corporate colour palettes for ggplot2"
  3. Access to common reference tables

Installation

You can install the development version of opmitools from GitHub with:

# install.packages("devtools")
devtools::install_github("MassDOT-OPMI/opmitools")

And load/attach opmitools as you would any other package with:

library(opmitools)

Usage

GTFS Files

Read the current MBTA GTFS from mbta.com with:

mbta_gtfs <- read_mbta_gtfs()

read_mbta_gtfs() is a wrapper for tidytransit::read_gtfs(). It stores the individual tables in a list and enforces column types as specified in the GTFS standard.

For some downstream trip planners and other applications, it is useful/necessary to remove all extraneous stops, shapes, routes, and service_ids (i.e., those that are not used in the either trips.txt or stop_times.txt). You can check for extraneous components with gtfs_removal_check():

gtfs_removal_check(mbta_gtfs)

And remove those components with gtfs_remove_all():

route_count_pre <- length(mbta_gtfs$routes$route_id)
mbta_gtfs <- gtfs_remove_all(mbta_gtfs)
route_count_post <- length(mbta_gtfs$routes$route_id)
paste0("Route count prior to removal: ", route_count_pre)
paste0("Route count after removal: ", route_count_post)

Other use cases might include getting some useful information out of the calendar.txt file:

mbta_calendar_info <- gtfs_calendar_info(mbta_gtfs)
head(mbta_calendar_info)

Note that opmitools is not yet capable of handling GTFS-Pathways -- it is only set up to handle the required GTFS files used by the MBTA: agency, calendar, routes, shapes, stop_times, stops, and trips. While some functions will work without tidytransit, it is recommended to install and attach tidytransit in conjunction with opmitools.

Plotting

opmitools contains several convenience functions for making "OPMI-themed" plots. As an example, we can plot information from rt_ridership, which is included in the package.

The following code creates a simple ggplot showing fall average weekday ridership on rapid transit.

library(tidyverse) # load & attach tidyverse for data wrangling with dplyr and plotting with ggplot2

(ridership_plot <-
    rt_ridership %>%  
    filter(day_type_name == "weekday") %>% 
    ggplot(aes(x = rating, y = tot_ons, fill = route_id)) +
    geom_col(position = position_dodge()))

Then, we can add the OPMI theme with theme_opmi():

(ridership_plot <-
   ridership_plot +
   labs(x = "Rating", y = "Average\nWeekday\nBoardings") +
   theme_opmi())

And use colors that make sense with scale_fill_opmi():

ridership_plot +
  scale_fill_opmi(palette = "rt", guide = "none")

Included Data

opmitools contains several reference tables that may be useful, including a table describing the MBTA service area:

head(mbta_service_area)

And frequently used demographic information by MA block group:

head(mabgs_19)


kmeakinmbta/opmitools documentation built on Oct. 25, 2023, 10:34 a.m.