astrotuRf
plots field hockey fields, upon which location data (e.g. XY co-ordinates of shots) can be graphed.
First, you will need to download the astrotuRf
package from my GitHub.
devtools::install_github("ChrisAFry/astrotuRf")
Next, open ggplot2
and ggforce
(install using install.packages("package")
before opening if you don't already have them) and astrotuRf
.
library(ggplot2)
library(ggforce)
library(astrotuRf)
astrotuRf
has 3 functions: plotting a field hockey field, plotting the top half of a field hockey field, and plotting the bottom half of a field hockey field.
Each function requires 7 inputs: background_color, turf_color, line_color, goal_color, line_width, p_spot_size, and turf_opacity. This seems excessive, but it is so that you have complete control over the look of the field that you want to plot.
# Plot a grey background, with a green astroturf, white lines, white goals, line widths of .5, penalty spots of size .75 and no transparency of the astroturf.
ggplot() + hockey_field("#bfbfbf", "#7CA867", "#ffffff", "#ffffff", .5, .75, 1)
Now lets plot the same field as above, but reduce the opacity of the turf.
# Plot a grey background, with a green astroturf, white lines, white goals, line widths of .5, penalty spots of size .75 and 75% transparency of the astroturf.
ggplot() + hockey_field("#bfbfbf", "#7CA867", "#ffffff", "#ffffff", .5, .75, .25)
# Plot the top half of the field, with a grey background, a blue astroturf, white lines, a white goal, line widths of 2, penalty spots of size 2, and no transparency.
ggplot() + hockey_field_top_half("#bfbfbf", "#3a3de7", "#ffffff", "#ffffff", 2, 2, 1)
# Plot the bottom half of the field, with a black background, with black astroturf, pink lines, a blue goal, line widths of 1, penalty spots of size 2, and no transparency.
ggplot() + hockey_field_bottom_half("#060606", "#060606", "#ff00e7", "#00ecff", 1, 2, 1)
ggplot2 works by layering components. Therefore, in most situations you will want to add the hockey field before you plot your data.
mydata <- data.frame(x = c(26, 35, 27, 21, 20), y = c(89, 80, 78, 87, 82))
ggplot(mydata) + hockey_field("#bfbfbf", "#7CA867", "#ffffff", "#ffffff", .5, .75, 1) + geom_point(aes(x, y))
However, the turf opacity allows you to make the turf completely transparent so that only the lines of the field are visible (which you could then plot on top of your data). This will be great for heatmaps.
# Imported an excel spreadsheet from my desktop called 'newdata', which had 2 columns: x, y.
ggplot(newdata) + stat_density_2d(aes(x, y, fill = ..density..), geom = "raster", contour = FALSE) + hockey_field("#bfbfbf", "#7CA867", "#ffffff", "#ffffff", .5, .75, 0)
These 2 packages for plotting soccer fields were helpful in developing my own package: 1 - https://github.com/Torvaney/ggsoccer 2 - https://github.com/FCrSTATS/SBpitch
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.