Skip to content

Instantly share code, notes, and snippets.

@aaronschiff
Created April 18, 2018 00:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronschiff/ed0fc7331ec1013cf270a1d55db59c56 to your computer and use it in GitHub Desktop.
Save aaronschiff/ed0fc7331ec1013cf270a1d55db59c56 to your computer and use it in GitHub Desktop.
Make a Japanese-style timetable using AT public transport data
# Make a Japanese-style timetable using AT public transport data
# You'll need to download AT's GTFS data file first from https://at.govt.nz/about-us/at-data-sources/google-transit-feed/
library(magrittr)
library(tidyverse)
library(hms)
library(lubridate)
library(stringr)
library(ggthemes)
# Load data
calendar <- read_csv("gtfs/calendar.txt") %>%
mutate(start_date = ymd(start_date),
end_date = ymd(end_date))
routes <- read_csv("gtfs/routes.txt")
stop_times <- read_csv("gtfs/stop_times.txt",
col_types = "cccciciic")
stops <- read_csv("gtfs/stops.txt")
trips <- read_csv("gtfs/trips.txt")
# Get some data for a stop
my_stop_code <- 3983
my_stop <- stops %>%
filter(stop_code == my_stop_code)
my_stop_dat <- stop_times %>%
filter(stop_id %in% my_stop$stop_id) %>%
left_join(trips, by = "trip_id") %>%
left_join(routes, by = "route_id") %>%
left_join(calendar, by = "service_id")
# Pick some routes and generate the timetable data
my_routes <- c("973", "974", "972", "971")
my_date <- today(tzone = "Pacific/Auckland")
my_timetable <- my_stop_dat %>%
filter(route_short_name %in% my_routes,
start_date <= my_date,
end_date >= my_date)
my_timetable_weekdays <- my_timetable %>%
filter(saturday == 0, sunday == 0) %>%
mutate(departure_time = as.hms(departure_time)) %>%
arrange(departure_time)
my_times_weekdays <- my_timetable_weekdays %>%
select(departure_time, route_short_name) %>%
mutate(hour = hour(departure_time),
minute = minute(departure_time)) %>%
mutate(hour_text = str_pad(hour, 2, pad = "0"),
minute_text = str_pad(minute, 2, pad = "0"),
ypos = max(hour) - hour + 1) %>%
group_by(hour) %>%
mutate(xpos = row_number()) %>%
ungroup()
# Plot the timetable
tt_plot <- ggplot(my_times_weekdays) +
geom_text(aes(x = 0, y = ypos, label = hour_text), fontface = "bold") +
geom_text(aes(x = xpos, y = ypos, label = minute_text)) +
geom_vline(xintercept = 0.5) +
ggtitle("To Britomart (buses 971, 972, 973, 974)") +
theme_foundation() +
theme(
plot.background = element_rect(fill = "white", colour = "white"),
axis.line.y = element_blank(),
axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(colour = "white")
)
print(tt_plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment