Created
November 3, 2022 06:01
-
-
Save jonocarroll/590b03bc5c3acb34947e62b20c778f19 to your computer and use it in GitHub Desktop.
Auckland Bus Cancellations plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://notstatschat.rbind.io/2022/11/03/improving-a-graph/ | |
d <- read.table( | |
"https://gist.githubusercontent.com/tslumley/9ac8df14309ecc5936183de84b57c987/raw/9ebf665b2ff9a93c1dbc73caf5ff346909899827/busdata.txt", | |
header = TRUE | |
) | |
d$date <- as.Date(paste(2022, d$mo, d$d, sep = "-")) | |
d$weekend <- with(d, weekdays(d$date) %in% c("Saturday", "Sunday")) | |
d$workday <- with(d, !(weekdays(date) %in% c("Saturday", "Sunday"))) | |
d$workday[d$day == 24 & d$mo == 10] <- FALSE | |
d$workday[d$day == 26 & d$mo == 9] <- FALSE | |
d$label <- "" | |
d$label[d$day == 24 & d$mo == 10] <- "Labour Day" | |
d$label[d$day == 26 & d$mo == 9] <- "QEII Memorial" | |
d$workday_c <- ifelse(d$workday, d$cancels, NA) | |
d$weekend_c <- ifelse(d$weekend, d$cancels, NA) | |
library(ggplot2) | |
library(ggtext) | |
ggplot(d) + | |
geom_point(aes(date, workday_c, col = workday)) + | |
geom_point(aes(date, weekend_c, col = workday)) + | |
geom_point(data = d[d$label != "",], aes(date, cancels, col = workday)) + | |
geom_line(aes(date, workday_c, col = workday, group = 1), na.rm = TRUE) + | |
geom_line(aes(date, weekend_c, col = workday, group = 1), na.rm = TRUE) + | |
scale_color_manual(values = c(`TRUE` = "steelblue", `FALSE` = "orangered")) + | |
scale_x_date(breaks = "week", date_labels = "%b %d") + | |
coord_cartesian(ylim = c(200, 2200)) + | |
ggrepel::geom_label_repel(aes(date, cancels, label = label, col = workday), family = "serif") + | |
labs(title = "Auckland Bus Cancellations on <span style = 'color:steelblue;'>Workdays</span> and <span style = 'color:orangered;'>Weekends/Holidays</span>", | |
x = "", y = "Cancelled Trips") + | |
theme_bw() + | |
theme( | |
plot.title = element_markdown(family = "serif"), | |
text = element_text(family = "serif"), | |
aspect.ratio = 0.4, | |
plot.background = element_rect(fill = "floralwhite"), | |
panel.background = element_rect(fill = "grey95") | |
) + | |
ggeasy::easy_rotate_x_labels(angle = 45, side = "right") + | |
guides(color = "none") |
Author
jonocarroll
commented
Nov 3, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment