Setup

library(tidyverse) # ggimage
source("../mytheme.R")
records <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-05-25/records.csv')
#drivers <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-05-25/drivers.csv')
records

Formatting

mytheme
## function (angle = c(0.5, 35), legend.position = "bottom") 
## {
##     ggplot2::theme(text = element_text(family = "serif"), panel.background = element_blank(), 
##         axis.ticks = element_blank(), plot.title = element_text(hjust = 0.5), 
##         plot.subtitle = element_text(hjust = 0.5), plot.caption = element_text(hjust = 0), 
##         legend.position = legend.position, legend.background = element_blank(), 
##         legend.key = element_blank(), axis.text.x = element_text(vjust = angle[1], 
##             hjust = 0, angle = angle[2]))
## }
# https://fantendo.fandom.com/wiki/Mario/Gallery
MarioWalk <- "https://static.wikia.nocookie.net/fantendo/images/c/ce/Mario_-_SuperMarioRun.png/revision/latest/scale-to-width-down/150?cb=20161102113357"
MarioFlip <- "https://static.wikia.nocookie.net/fantendo/images/2/23/Mario_2_-_SuperMarioRun.png/revision/latest/scale-to-width-down/133?cb=20161115150512"
MarioFly <- "https://static.wikia.nocookie.net/fantendo/images/b/b4/Mario_3_-_SuperMarioRun.png/revision/latest/scale-to-width-down/160?cb=20161115150517"
MarioHat <- "https://static.wikia.nocookie.net/fantendo/images/3/3f/Mario_artwork_%28Mario_Party_10%29.png/revision/latest/scale-to-width-down/185?cb=20150111165724"

# https://www.schemecolor.com/super-mario-colors.php#:~:text=Super%20Mario%20Logo%20Colors%20with%20Hex%20%26%20RGB,37%2C%203%20...%20%201%20more%20rows%20
MarioPal <- c("#049CD8", "#FBD000", "#E52521", "#43B047")
icon <- data.frame(x = seq(4, 1, -1),
                   y = c(35, 40, 47, 105), # found by eye
                   image = c(MarioWalk, MarioHat, MarioFlip, MarioFly))

Plots

Which track is the fastest?

records %>%
  group_by(track, shortcut_type = paste(shortcut, type, sep = ", ")) %>%
  summarize(median = median(time)) %>%
  group_by(shortcut_type) %>% arrange(median) %>% mutate(rank = row_number()) %>%
  filter(rank < 4) %>% mutate(rank = factor(rank, levels = c(3, 2, 1))) %>%
  
  ggplot(aes(x = reorder(shortcut_type, -median), y = median, fill = rank)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label = track, y = median), position = position_dodge(width = .99),
            hjust = 1.1, size = 3.3, family = "serif") +
  coord_flip() +
  scale_fill_manual(values = MarioPal) +
  scale_x_discrete(labels = c("No Shortcuts\nThree Laps", "Shortcuts\nSingle Lap",
                              "No Shortcuts\nSingle Lap", "Shortcuts\nThree Laps")) +
  xlab(NULL) + ylab("\nMedian Race Times (sec)") +
  ggimage::geom_image(data = icon, aes(x = x, y = y, image = image), size = .1,
             inherit.aes = FALSE, hjust = 1.1) +
  ggtitle("Fastest Mario Kart Tracks") +
  mytheme(angle = NULL, legend.position = "none")

On which track does the shortcut save the most time?

records %>%
  filter(type == "Three Lap") %>%
  group_by(track, shortcut) %>% summarize(median = median(time)) %>% 
  
  pivot_wider(id_cols = track, names_from = shortcut, values_from = median) %>% na.omit() %>%
  mutate(diff = No - Yes) %>%
  pivot_longer(cols = c(Yes, No), names_to = "shortcut", values_to = "median") %>%
  
  ggplot(aes(x = reorder(track, diff), y = median)) +
  geom_line(aes(group = track),
            #arrow = arrow(ends = "first", angle = 15, type = "closed", length = unit(0.05, "npc")),
            color = "grey") +
  geom_point(aes(color = shortcut)) +
  
  scale_color_manual(name = "", values = MarioPal[2:3]) +
  geom_text(aes(x = track, y = 0, label = round(diff * -1, digits = 2)), nudge_y = -5, family = "serif", size = 3) +
  
  ggtitle("Impact of Shortcuts on Duration of\nThree-lap Races\n") +
  xlab(NULL) + ylab("\nMedian Race Time (sec)") + labs(caption = "\n*Tracks without shortcuts not included.") +
  
  coord_flip() + mytheme(angle = NULL)