Tutorial - Plotting Shots in Power Bi using R
Hi all, last time we created a nice shot plot using the built-in scatter plot for Power Bi. Today we will take things a step further, using the R Custom Visual in Power Bi. This visual allows you to use R code to create a visual however you want, with that visual interacting with the slicers on your page. Use base ggplot2, these visuals will not have any interaction ability, such as tooltips, but they provide a neat way of creating visuals not possible with the built-in set provided by Microsoft.
As always, subscribe here for more videos and to Power Performance Through Data.
Steps for creating the visual
The steps for creating this plot are relatively simple:
- Add the R Visual to the canvas.
- If you haven’t already, you will need to enable R Scripts for Power Bi desktop and have a version of R installed on your computer.
- Add all relevant fields to the visualisation pane.
- Elapsed time used to make sure co-ordinates do not sum together or shots are not missed.
- Location.y is added to the x-axis as the pitch is rotated.
- Location.x is added to the y-axis value.
- Shot.outcome.name is added as a tooltip and to the legend for colour.
- Add the code required to create the visual, including all required packages.
- This should be set out how you would normally create a visual in R Studio or another IDE.
- Click the play button to run the code and create the visual.
Thngs to remember
This visual will be more accurate compared to the basic version we created last time. The co-ordinates are plotted on the pitch accurately. This pitch is also recreated based on the co-ordinates provided by StatsBomb, so are accurate to what they are providing.
The size and shape type should be adjusted to make the dots more visible on the pitch background.
The colours of the dots will need to be changed using scale-fill-manual. This allows you to define the breaks and colours applied to those breaks to accurately match the legend for the visual.
Hope you found this tutorial helpful. If you have any questions please don’t be afraid to reach out below.