This past weekend, myself and a friend of mine competed in a triathlon in downtown Indianapolis. The group that organizes the race does a great service by publishing all of the athletes’ final times along with event splits in one big table after the race. Having done a few of these races now, I thought it would be an interesting challenge to try to “visualize” the race data. It’s a perfect opportunity for an animated visualization, because everyone is competing over the same distance; the only difference across athletes is pace and finish time. Luckily, using the fantastic gganimate package, it’s easy to create visualizations that represent progress over time in R. Below is a representation of my time in comparison to the whole field of athletes (in gray) and those in my age/gender group (in orange). And below is the data and ggplot2 + gganimate code to make it work!
ggplot & gganimate code for this animation
You can get the cleaned data set here.
library(tidyverse) library(gganimate) library(lubridate) ggplot() + # use data for all race competitors for grey lines geom_line(data=plotdat,aes(x=cumulative_time,y=dist,group=name),alpha=0.25,color="grey") + # use the data just for my group for comparison in orange geom_line(data=grpdat,aes(x=cumulative_time,y=dist,group=name),color="orange",alpha=0.25) + # plot my data only in blue geom_line(data=natedat,aes(x=cumulative_time,y=dist,group=name),color="blue",size=1) + # add the label for me geom_label(data=natedat,aes(x=cumulative_time,y=dist,label="Me!"),color="blue") + # theme adjustments, titles for axes and plot theme_bw() + labs(x="Cumulative Race Time Elapsed",y="Cumulative Distance (m)", title="Tri Indy 2019 Sprint Distance Race Results", subtitle="Orange shows all other age/gender group members") + # add breaks for race stages scale_y_continuous(breaks=c(500,20500,25500), labels = c("Swim Finish","Bike Finish","Run/Race Finish")) + # add breaks for time increments scale_x_continuous(breaks = c(1800,3600,5400,7200,9000), labels = c("30min","1h","1:30","2h","2:30")) + # add the gganimate code to reveal the lines over time transition_reveal(cumulative_time)