Skip to content

Instantly share code, notes, and snippets.

@PeskyPotato
Last active March 24, 2024 11:19
Show Gist options
  • Save PeskyPotato/591716eb624fdf7b97f38e22b883e03e to your computer and use it in GitHub Desktop.
Save PeskyPotato/591716eb624fdf7b97f38e22b883e03e to your computer and use it in GitHub Desktop.
Create rail timetable graphs in R using ggplot2
Stations Distance 7112 7114 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7127 7129 7131 7133 7214 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7231 7233
Dordrecht 0 06:54:00 07:24:00 07:54:00 06:01:00 08:24:00 06:31:00 08:54:00 07:01:00 09:24:00 07:31:00 09:54:00 08:01:00 08:31:00 09:01:00 09:31:00 10:01:00 07:09:00 07:39:00 05:46:00 08:09:00 06:16:00 08:39:00 06:46:00 09:09:00 07:16:00 09:39:00 07:46:00 10:09:00 08:16:00 10:39:00 08:46:00 09:16:00 09:46:00
Dordrecht 0 06:54:00 07:24:00 07:54:00 06:01:00 08:24:00 06:31:00 08:54:00 07:01:00 09:24:00 07:31:00 09:54:00 08:01:00 08:31:00 09:01:00 09:31:00 10:01:00 07:09:00 07:39:00 05:46:00 08:09:00 06:16:00 08:39:00 06:46:00 09:09:00 07:16:00 09:39:00 07:46:00 10:09:00 08:16:00 10:39:00 08:46:00 09:16:00 09:46:00
Dordrecht - Stadspolders 3.4 06:50:00 07:20:00 07:50:00 06:04:00 08:20:00 06:34:00 08:50:00 07:04:00 09:20:00 07:34:00 09:50:00 08:04:00 08:34:00 09:04:00 09:34:00 10:04:00 07:05:00 07:35:00 05:49:00 08:05:00 06:19:00 08:35:00 06:49:00 09:05:00 07:19:00 09:35:00 07:49:00 10:05:00 08:19:00 10:35:00 08:49:00 09:19:00 09:49:00
Dordrecht - Stadspolders 3.4 06:49:00 07:19:00 07:49:00 06:05:00 08:19:00 06:35:00 08:49:00 07:05:00 09:19:00 07:35:00 09:49:00 08:05:00 08:35:00 09:05:00 09:35:00 10:05:00 07:04:00 07:34:00 05:50:00 08:04:00 06:20:00 08:34:00 06:50:00 09:04:00 07:20:00 09:34:00 07:50:00 10:04:00 08:20:00 10:34:00 08:50:00 09:20:00 09:50:00
Sliedrecht Baanhoek 7.7 06:46:00 07:16:00 07:46:00 06:09:00 08:16:00 06:39:00 08:46:00 07:09:00 09:16:00 07:39:00 09:46:00 08:09:00 08:39:00 09:09:00 09:39:00 10:09:00 07:01:00 07:31:00 05:54:00 08:01:00 06:24:00 08:31:00 06:54:00 09:01:00 07:24:00 09:31:00 07:54:00 10:01:00 08:24:00 10:31:00 08:54:00 09:24:00 09:54:00
Sliedrecht Baanhoek 7.7 06:46:00 07:16:00 07:46:00 06:09:00 08:16:00 06:39:00 08:46:00 07:09:00 09:16:00 07:39:00 09:46:00 08:09:00 08:39:00 09:09:00 09:39:00 10:09:00 07:01:00 07:31:00 05:54:00 08:01:00 06:24:00 08:31:00 06:54:00 09:01:00 07:24:00 09:31:00 07:54:00 10:01:00 08:24:00 10:31:00 08:54:00 09:24:00 09:54:00
Sliedrecht 10.3 06:43:00 07:13:00 07:43:00 06:12:00 08:13:00 06:42:00 08:43:00 07:12:00 09:13:00 07:42:00 09:43:00 08:12:00 08:42:00 09:12:00 09:42:00 10:12:00 06:58:00 07:28:00 05:57:00 07:58:00 06:27:00 08:28:00 06:57:00 08:58:00 07:27:00 09:28:00 07:57:00 09:58:00 08:27:00 10:28:00 08:57:00 09:27:00 09:57:00
Sliedrecht 10.3 06:42:00 07:12:00 07:42:00 06:13:00 08:12:00 06:43:00 08:42:00 07:13:00 09:12:00 07:43:00 09:42:00 08:13:00 08:43:00 09:13:00 09:43:00 10:13:00 06:57:00 07:27:00 05:58:00 07:57:00 06:28:00 08:27:00 06:58:00 08:57:00 07:28:00 09:27:00 07:58:00 09:57:00 08:28:00 10:27:00 08:58:00 09:28:00 09:58:00
Hardinxveld - Blauwe Zoom 12.9 06:55:00 07:25:00 06:00:00 07:55:00 06:30:00 08:25:00 07:00:00 08:55:00 07:30:00 09:25:00 08:00:00 09:55:00 08:30:00 10:25:00 09:00:00 09:30:00 10:00:00
Hardinxveld - Blauwe Zoom 12.9 06:55:00 07:25:00 06:01:00 07:55:00 06:30:00 08:25:00 07:00:00 08:55:00 07:30:00 09:25:00 08:00:00 09:55:00 08:30:00 10:25:00 09:00:00 09:30:00 10:00:00
Hardinxveld - Giessendam 14.3 06:39:00 07:09:00 07:39:00 06:16:00 08:09:00 06:46:00 08:39:00 07:16:00 09:09:00 07:46:00 09:39:00 08:16:00 08:46:00 09:16:00 09:46:00 10:16:00 06:53:00 07:23:00 06:02:00 07:53:00 06:32:00 08:23:00 07:02:00 08:53:00 07:32:00 09:23:00 08:02:00 09:53:00 08:32:00 10:23:00 09:02:00 09:32:00 10:02:00
Hardinxveld - Giessendam 14.3 06:39:00 07:09:00 07:39:00 06:16:00 08:09:00 06:46:00 08:39:00 07:16:00 09:09:00 07:46:00 09:39:00 08:16:00 08:46:00 09:16:00 09:46:00 10:16:00 06:53:00 07:23:00 06:03:00 07:53:00 06:33:00 08:23:00 07:03:00 08:53:00 07:33:00 09:23:00 08:03:00 09:53:00 08:33:00 10:23:00 09:03:00 09:33:00 10:03:00
Boven – Hardinxveld 17.3 06:36:00 07:06:00 07:36:00 06:19:00 08:06:00 06:49:00 08:36:00 07:19:00 09:06:00 07:49:00 09:36:00 08:19:00 08:49:00 09:19:00 09:49:00 10:19:00 06:49:00 07:19:00 06:06:00 07:49:00 06:36:00 08:19:00 07:06:00 08:49:00 07:36:00 09:19:00 08:06:00 09:49:00 08:36:00 10:19:00 09:06:00 09:36:00 10:06:00
Boven – Hardinxveld 17.3 06:35:00 07:05:00 07:35:00 06:20:00 08:05:00 06:50:00 08:35:00 07:20:00 09:05:00 07:50:00 09:35:00 08:20:00 08:50:00 09:20:00 09:50:00 10:20:00 06:48:00 07:18:00 06:06:00 07:48:00 06:36:00 08:18:00 07:06:00 08:48:00 07:36:00 09:18:00 08:06:00 09:48:00 08:36:00 10:18:00 09:06:00 09:36:00 10:06:00
Gorinchem 23.6 06:30:00 07:00:00 07:30:00 06:25:00 08:00:00 06:55:00 08:30:00 07:25:00 09:00:00 07:55:00 09:30:00 08:25:00 08:55:00 09:25:00 09:55:00 10:25:00 06:43:00 07:13:00 06:12:00 07:43:00 06:42:00 08:13:00 07:12:00 08:43:00 07:42:00 09:13:00 08:12:00 09:43:00 08:42:00 10:13:00 09:12:00 09:42:00 10:12:00
Gorinchem 23.6 06:30:00 07:00:00 07:30:00 06:25:00 08:00:00 06:55:00 08:30:00 07:25:00 09:00:00 07:55:00 09:30:00 08:25:00 08:55:00 09:25:00 09:55:00 10:25:00 06:36:00 07:06:00 06:14:00 07:36:00 06:44:00 08:06:00 07:14:00 08:36:00 07:44:00 09:06:00 08:14:00 09:36:00 08:44:00 10:06:00 09:14:00 09:44:00 10:14:00
Arkel 28.4 06:32:00 07:02:00 06:19:00 07:32:00 06:49:00 08:02:00 07:19:00 08:32:00 07:49:00 09:03:00 08:19:00 09:32:00 08:49:00 10:02:00 09:19:00 09:49:00 10:19:00
Arkel 28.4 06:32:00 07:02:00 06:19:00 07:32:00 06:49:00 08:02:00 07:19:00 08:32:00 07:49:00 09:02:00 08:19:00 09:32:00 08:49:00 10:02:00 09:19:00 09:49:00 10:19:00
Leerdam 35.8 06:26:00 06:56:00 06:25:00 07:26:00 06:55:00 07:56:00 07:25:00 08:26:00 07:55:00 08:56:00 08:25:00 09:26:00 08:55:00 09:56:00 09:25:00 09:55:00 10:25:00
Leerdam 35.8 06:25:00 06:55:00 06:26:00 07:25:00 06:56:00 07:55:00 07:26:00 08:25:00 07:56:00 08:55:00 08:26:00 09:25:00 08:56:00 09:55:00 09:26:00 09:56:00 10:26:00
Beesd 42.9 06:19:00 06:49:00 06:31:00 07:19:00 07:01:00 07:49:00 07:31:00 08:19:00 08:01:00 08:49:00 08:31:00 09:19:00 09:01:00 09:49:00 09:31:00 10:01:00 10:31:00
Beesd 42.9 06:19:00 06:49:00 06:32:00 07:19:00 07:02:00 07:49:00 07:32:00 08:19:00 08:02:00 08:49:00 08:32:00 09:19:00 09:02:00 09:49:00 09:32:00 10:02:00 10:32:00
Geldermalsen 49.4 06:14:00 06:44:00 06:37:00 07:14:00 07:07:00 07:44:00 07:37:00 08:14:00 08:07:00 08:44:00 08:37:00 09:14:00 09:07:00 09:44:00 09:37:00 10:07:00 10:37:00
Geldermalsen 49.4 06:14:00 06:44:00 06:37:00 07:14:00 07:07:00 07:44:00 07:37:00 08:14:00 08:07:00 08:44:00 08:37:00 09:14:00 09:07:00 09:44:00 09:37:00 10:07:00 10:37:00
library(ggplot2)
readfile <- read.csv("merwedelingelijn-timetable.csv", check.names = FALSE)
# get train series labels, removes first two columns
train_series <- names(readfile)
train_series <- train_series[-1]
train_series <- train_series[-1]
# get number of train stops for each train at each station
# id_order contains stops of each train series
# location_order maps each stop to the train series id
# times map the times of the stops to the train series id
id_order <- c()
distances_order <- c()
times <- c()
for (i in seq_along(train_series)) {
station_number <- 1L
for (station in readfile$Stations) {
if (readfile[station_number, i + 2] != "") {
id_order <- append(id_order, train_series[i])
times <- append(times,
paste("2024-03-11", readfile[station_number, i + 2]))
distances_order <- append(distances_order, readfile[station_number, 2L])
}
station_number <- station_number + 1L
}
}
time <- structure(as.POSIXct(times),
class = c("POSIXct", "POSIXt"), tzone = "CET")
df <- data.frame(series = id_order,
times = time,
distances = distances_order)
ggplot(df, aes(x = times, y = distances, group = series)) +
geom_line(aes(colour = series)) +
scale_y_continuous(
breaks = unique(readfile$Distance), labels = unique(readfile$Stations)
) +
scale_x_datetime(breaks = "30 min", date_labels = "%H:%M") +
ggtitle("MerwedeLingelijn trains") +
labs(colour = "Train series", y = "Station", x = "Time")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment