Skip to content

Instantly share code, notes, and snippets.

@meghall06
Created July 19, 2022 00:08
Show Gist options
  • Save meghall06/d6bf87bb47dad27debcfcb0a88b4dd88 to your computer and use it in GitHub Desktop.
Save meghall06/d6bf87bb47dad27debcfcb0a88b4dd88 to your computer and use it in GitHub Desktop.
# to make the plot shown here: https://twitter.com/MeghanMHall/status/1549183367769366537
library(f1dataR)
library(tidyverse)
library(cowplot)
drivers <- load_drivers(season = 2022) %>%
filter(code != "HUL")
fastestlaps <- function(dr) {
get_driver_telemetry(2022, 11, driver = dr, fastest_only = TRUE)
}
austria <- purrr::map_df(unique(drivers$code), fastestlaps)
braked <- austria %>%
group_by(driverId) %>%
mutate(index = row_number(),
dist_lapsed = lead(Distance) - Distance) %>%
group_by(driverId, Brake) %>%
summarize(dist = sum(dist_lapsed, na.rm = TRUE)) %>%
filter(Brake == TRUE)
bar <- braked %>%
mutate(diff = dist - 521.8,
label = str_c("+", round(diff, 0), "m")) %>%
ggplot(aes(y = reorder(driverId, -dist), x = diff)) +
geom_bar(stat = "identity", fill = "#808080") +
geom_text(aes(label = label), size = 3, hjust = -0.1) +
scale_x_continuous(expand = expansion(mult = c(0, .16))) +
theme_minimal() +
theme(axis.ticks = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank())
map <- austria %>%
filter(driverId %in% c("RIC","BOT")) %>%
mutate(val = str_c(driverId, Brake)) %>%
ggplot(aes(X, Y, color = as.factor(val), alpha = as.factor(val), group = Time)) +
scale_color_manual(values = c("grey","#440154","grey","#FDE725")) +
scale_alpha_manual(values = c(0.1, 1, 0.1, 1)) +
scale_x_continuous(limits = c(-12000, 4500)) +
geom_path(size = 4, lineend = 'round') +
theme_void() +
theme(legend.position = "none")
ggdraw(map) +
draw_plot(bar, x = 0, y = 0, width = .5, height = .75) +
draw_label("Of the drivers' fastest\nlaps at the 2022\nAustrian GP,\nRicciardo had the\nshortest total braking\ndistance (521m)",
x = 0.85, y = 0.8, fontfamily = "Avenir") +
draw_label("Total braking distance\non fastest lap, compared to RIC",
x = 0.02, y = 0.78, size = 9, hjust = 0, fontfamily = "Avenir") +
draw_label("Viz by @MeghanMHall,\ndata from {f1dataR}",
x = 0.78, y = 0.08, size = 9, hjust = 0, fontfamily = "Avenir") +
draw_label("Bottas's braking (purple)\ncompared to\nRicciardo's (yellow)",
x = 0.72, y = 0.35, size = 12, fontfamily = "Avenir")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment