Interactive Figures for Sport Science in R

Interactive figures can have great use in sport science, from visualising training reports to communicating test scores. One R package that allows the creating of interactive figures, using R code, isĀ plotly.

To demonstrate how plotly works and how it may be of use to visualise your own dataset, consider the example below.

First, we create dummy data:

# To create interactive figures - declare netball playing position
PlayingPosition = c("C", "WA", "WD", "GA", "GD", "GS", "GK")
# Declare four seperate training sessions
TrainingSession = c("Main Training", "Courtwork", "Match Simulation", "Long Court")
# Set the seed, to reuse the same set of distance variables
# Create a summary data.frame containing dummy total and sprint distance data, across all training sessions
PhysicalOutputData = data.frame(PlayingPosition = rep((PlayingPosition), each = 4),
TrainingSession = rep((TrainingSession), each = 1),
TotalDistance = runif(28, 450, 950),
SprintDistance = runif(28, 0, 200))
# Round the total and sprint distance columns to 1 decimal place
PhysicalOutputData$TotalDistance <- round(PhysicalOutputData$TotalDistance, digits = 1)
PhysicalOutputData$SprintDistance <- round(PhysicalOutputData$SprintDistance, digits = 1)

Next, we load a package called dplyr, that is very useful once you start to use R reguarly! This package allows you to manipulate data.frames to quickly calculate means and SDs. You can see an example of this in our next step below:

# As an example, create a summary column looking at weekly totals
WeekSummaryData <- PhysicalOutputData %>%
group_by(PlayingPosition) %>%
summarise(TD = sum(TotalDistance),
SD = sum(SprintDistance))

Now we load the plotly package (you may need to install this first in R) and using the code below, create our first interactive figure!

# Load required package
# Plot - basic plotly image
plot_ly(data = WeekSummaryData, x = ~TD, y = ~PlayingPosition, type = 'bar', orientation = 'h',
color = ~ PlayingPosition) %>%
layout(title = "Weekly Total Distance Covered",
yaxis = list(title = ""),
xaxis = list(title = "Total Distance (m)"),
showlegend = FALSE)


The plot above is a static plot but in the “Plots” tab of R, you should now be able to hover over the plot, with each position and the total distance covered for the week displayed.

To change the hover information, use the code below to not only display the total distance but the total sprint distance covered.

# To alter the hover text - show the sprint distance covered
plot_ly(WeekSummaryData, x = ~TD, y = ~PlayingPosition,
type = 'bar',
orientation = 'h',
hoverinfo = 'text',
text = ~paste('Total Distance:', TD,'m',
' ',
'Sprint Distance:', SD, 'm'),
color = ~ PlayingPosition) %>%
layout(title = "Weekly Total Distance Covered",
yaxis = list(title = ""),
xaxis = list(title = "Total Distance (m)"),
showlegend = FALSE)

Use the above code as a template for your own reporting – which you can now visualise a great deal of information in just one figure!