library(knitr) read_chunk('./CodaBonito.R')
This document offers a brief introduction to the functions in this library. Some function might be fairly straightforward to create but some functions may need you to look up the help entry before trying it out.
This library is very much in development. It is a compilation of code I've written across different project which is why there may be syntactical inconsistencies, such as some functions using player names and some others using player IDs as a reference for players. Some of this is also code that I have never published the output of so it may need additional arguments. I have, however, tried to give enough documentation for each function so anyone trying to use the library should be well equipped with instructions and a basic understanding of how the functions work.
Any inputs and feedback is welcome. If you're on github, then head to github.com/thecomeonman/CodaBonit, or else get in touch on Twitter
Install R from https://cran.r-project.org
Open R and run this command in the console -
install.packages("devtools");
library(devtools);
install_github("thecomeonman/CodaBonito");
And you're ready to run the examples below!
I have added some fake data along with the package to be able to better explain the usage of these functions.
dtPlayerMetrics
- aggregated data for players is typically in a format
similar to this, with some extra details about the team they play for, their
age, etc.
dtMetricCategorisation
- some metadata about the metrics.
- variableLabel is the name that will be displayed in charts for that metric,
- variableCategory is the grouping of variables used in some visualisations,
like fNormalisedValueChart
- HighValueIsBad is marked true for variables where a high value is bad.
Variables such as fouls and goals conceded would be true.
dtPasses
- passing data.
- x,y denote the start coordinates of the pass
- endX, endY denote the end coordinates of the pass
- passLength is the length of the pass
- passAngle is the angle of the pass in radians ( 180 degress = pi radians )
where 0 is along the pitch from defense to offense.
- Success 1 for successful pass, 0 for failed pass
dtFormation
- Coordinates as per the formation
dtPlayerLabels
- Player labels
lTrackingData$dtTrackingData
- Tracking data
fAddPitchLines draws pitch markings with further customisations available
You can add whatever stats you want on top of it like regular ggplot2
If you aren't interested in having the axis markings, etc., use theme_pitch
Oh, do you want the pitch from a different perspective?
The pass origins are points so they don't look as nicely mapped to the surface but if you were to use a 2+ dimensional shape, like a polygon, rectangle, cuboid, etc. then it would map well. The pitch, the goal frame, the net, all of that is also paths and polygons which is why it looks well fit in the 3D version.
Percentiles can be a little misleading if the underlying numbers aren't uniformly distributed. You can add annotations for an indicator of the absolute spread of the values and where this particular player's values fall within that spread.
I disapprove of radar charts. It's a bad visualisation, prone to misinterpretation. They seem to be the accepted norm of comparing players though which is why I had to sell out and have an implementation of that in the package, but I've added a warning which states how you should use one of the other visualisations instead as those are better structured visualisation.
WIP using the same data structure as https://github.com/metrica-sports/sample-data which you can parse with fParseTrackingDataBothTeams
You can draw Voronois
And if you have multiple frames -
The Friends of Tracking pitch control model -
Some of my other experiments with tracking data are here - https://github.com/thecomeonman/MakingFriendsWithTrackingData
A function to calculate earth mover's distance. It offers more flexibility and transparency than emdist:emd.
Any distance matrix can be used to calculated EMD, but emdist:emd insists on getting the raw distributions with only up to four dimensions. fEMDDetailed only requires a distance matrix between each combination of observations in the two datasets, irrespective of the nature of the data.
On the topic of transparency, one of the things I find very useful is that you can now see how much distance is being contributed by each observation.
You will find fJsonToListOfTables
, fJsonToTabular
, fParseTrackingData
useful. They aren't glamarous enough to be demoed here but the documentation should help you use those functions.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.