Running pace and distance over time

[This article was first published on R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

While running for a couple of years now (since 2004) I wanted to see if there is some progress in speed and distance. I didn’t log much in the beginning, no notebook or anything like that, but around 2010 I purchased my first GPS watch. This enables automatic logging! Anyway, this is the result of 10 years of running:

Below code demonstates how this is created. It creates an image for every year. These files are later combined in an animated gif. There are different ways to achieve this, without creating and saving the seperate images. Perhaps I’ll write something about that in the future.

Graphs are created using ggplot, the MASS library is required to create the polygones. Sport_df.Rda contain all data and is created with another script which gets data from Strava and converts it to the data frame which I use here.

In a loop a data frame is created for every year with distance in column x and pace in column y. this is reused every iteration. With the data in a stuctured in the way we like is we can now plot it using ggplot. 2D density lines will be added using stat_density2d from the MASS package.

Saving the plot as PNG with the prefix scat_den_run to be used later to filter these files from a directory with multiple plots.


jaar <- c(2011:2020)
for(i in jaar){
df_run <- data.frame("x"= Sport$dist[Sport$type=="Run" & Sport$year==i],
          "y"= Sport$pace[Sport$type=="Run" & Sport$year==i])

Run <- ggplot(data=df_run,aes(x,y)) + 
stat_density2d(aes(fill=..level..,alpha=..level..), geom="polygon",colour="black") + 
   scale_fill_continuous(low="green",high="red") +
   scale_y_reverse() +
   geom_point() + 
   xlim(1,45) +
   ylim(4,7) +
   labs(title="Running pace versus distance", subtitle = i)

# Print graphs
filename_run <- paste("scat_den_run",i,".png", sep="")

png(filename=filename_run, width=1000, height=400, bg="black", type="cairo")

Now all images have been created and placed in the directory. We only have to combine them into an animated gif using the fabulous magick package.

files <- list.files(path = ".", pattern = c("scat_den_run",".png"))
#get all images in a list
images <- map(files, image_read)
images <- image_join(images)
animation <- image_animate(image_scale(images), fps = 2, dispose = "previous")
image_write(animation, path="./animation_run.gif")

To leave a comment for the author, please follow the link and comment on their blog: R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)