## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(mtaOpenData)
library(ggplot2)
library(dplyr)

## ----mta-list-datasets--------------------------------------------------------
mta_list_datasets() |> head()

## ----mta-pull-----------------------------------------------------------------
mta_bus_stops_id <- mta_pull_dataset(
  dataset = "2ucp-7wg5", limit = 2)

mta_bus_stops_key <- mta_pull_dataset(
  dataset = "mta_bus_stops", limit = 2)

## ----filter-qm15--------------------------------------------------------------

qm15_info <- mta_pull_dataset(dataset = "2ucp-7wg5",limit = 2, filters = list(route_id = "QM15"))
qm15_info

# Checking to see the filtering worked
qm15_info |>
  distinct(route_id)

## ----tbl-qm15-17-info---------------------------------------------------------
qm15_17_info <- mta_pull_dataset(dataset = "2ucp-7wg5",limit = 2, filters = list(route_id = c("QM15","QM17")))
qm15_17_info

## ----tbl-qm15-17-info-in-effect-----------------------------------------------
qm15_17_info_in_effect <- mta_pull_dataset(dataset = "2ucp-7wg5",limit = 2, filters = list(route_id = c("QM15","QM17"), in_effect = "true"))
qm15_17_info_in_effect

## ----compaint-type-graph, fig.alt="Horizontal bar chart displaying the distribution of NYC bus route directions. The y-axis lists directions (e.g., Northbound, Southbound), and the x-axis shows the frequency of routes currently in effect for each. Bars are ordered by frequency to highlight which directions are most common in the dataset.", fig.cap="Distribution of active NYC bus route directions. Data retrieved via the mtaOpenData package from the MTA Bus Route info dataset (ID: 2ucp-7wg5).", fig.height=5, fig.width=7----
# Visualizing the distribution, ordered by frequency

mta_bus_info <- mta_pull_dataset(dataset = "2ucp-7wg5", filters = list(in_effect = "true"))

mta_bus_info |>
  count(direction) |>
  ggplot(aes(
    x = n,
    y = reorder(direction, n)
  )) +
  geom_col(fill = "steelblue") +
  theme_minimal() +
  labs(
    title = "Top Directions NYC Buses Go On Their Routes",
    x = "Count",
    y = "Direction"
  )

