Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joe Gallagher
committed
Jun 6, 2018
1 parent
c3cde09
commit 4a43dcd
Showing
19 changed files
with
365 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' @import ggplot2 | ||
#' @importFrom ggforce geom_arc geom_circle | ||
NULL | ||
#' Flips x,y-coordinates horizontally in one half to account for changing sides at half-time | ||
#' | ||
#' @description Normalises direction of attack in both halves of both teams by | ||
#' flipping x,y-coordinates horizontally in either the first or second half; | ||
#' i.e. teams attack in the same direction all game despite changing sides at | ||
#' half-time. | ||
#' | ||
#' @param dat = dataframe containing unnormalised x,y-coordinates named `x` and `y` | ||
#' @param periodVar = name of variable containing period labels | ||
#' @param periodToFlip = which period to flip | ||
#' @param pitchLength,pitchWidth = length, width of pitch in metres | ||
#' @return a dataframe | ||
#' @examples | ||
#' # to flip coordinates in 2nd half of a dataframe with 1st/2nd half identity labelled by variable named `period` | ||
#' soccerFlipHoriz(df, "period", 2, 105, 68) | ||
#' | ||
#' @export | ||
soccerFlipHoriz <- function(dat, periodVar = "period", periodToFlip = 1, pitchLength = 105, pitchWidth = 68) { | ||
|
||
dat$x <- ifelse(dat[,periodVar] == periodToFlip, pitchLength - dat$x, dat$x) | ||
dat$y <- ifelse(dat[,periodVar] == periodToFlip, pitchWidth - dat$y, dat$y) | ||
|
||
return(dat) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' @include soccerPath.R | ||
#' @import ggplot2 | ||
#' @importFrom ggforce geom_arc geom_circle | ||
NULL | ||
#' Normalises x,y-coordinates to metres units for use with soccermatics functions | ||
#' | ||
#' @description Normalises x,y-coordinates from between any arbitrary bounds to metre units bounded by [0 < x < pitchLength, 0 < y < pitchWidth] | ||
#' | ||
#' @param dat = dataframe containing unnormalised x,y-coordinates named `x` and `y` | ||
#' @param xMin,xMax,yMin,yMax = range of x,y-coordinates possible in the raw dataset | ||
#' @param pitchLength,pitchWidth = length, width of pitch in metres | ||
#' @return a dataframe | ||
#' @examples | ||
#' # Three examples with true pitch dimesions (in metres): | ||
#' lengthPitch <- 101 | ||
#' widthPitch <- 68 | ||
#' | ||
#' # Example 1. Opta-style ------------------------------------------------------- | ||
#' # limits = [0 < x < 100, 0 < y < 100] | ||
#' # centre of pitch = [50,50] | ||
#' | ||
#' df <- data.frame(t = 1:12, | ||
#' x = c(50,55,61,66,62,58,51,44,45,42,41,32), | ||
#' y = c(50,48,47,40,42,45,49,51,59,75,88,100)) | ||
#' | ||
#' df <- soccerNormXY(df, 0, 100, 0, 100, lengthPitch, widthPitch) | ||
#' | ||
#' soccerPath(df, lengthPitch = lengthPitch, widthPitch = widthPitch) | ||
#' | ||
#' | ||
#' # Example 2. StrataBet-style -------------------------------------------------- | ||
#' # limits = [0 < x < 420, -136 < y < 136] | ||
#' # centre of pitch = [210,0] | ||
#' | ||
#' df <- data.frame(t = 1:12, | ||
#' x = c(210,222,201,192,178,170,143,122,104,91,75,60), | ||
#' y = c(0,-5,-20,-12,-8,-2,4,8,13,20,30,45)) | ||
#' | ||
#' df <- soccerNormXY(df, 0, 420, -136, 136, lengthPitch, widthPitch) | ||
#' | ||
#' soccerPath(df, lengthPitch = lengthPitch, widthPitch = widthPitch) | ||
#' | ||
#' | ||
#' # Example 3. Other ------------------------------------------------------------ | ||
#' # limits = [-5250 < x < 5250, -3400 < y < 3400] | ||
#' # centre of pitch = [0,0] | ||
#' | ||
#' xMin <- -5250 | ||
#' xMax <- 5250 | ||
#' yMin <- -3400 | ||
#' yMax <- 3400 | ||
#' | ||
#' df <- data.frame(x = c(0,-452,-982,-1099,-1586,-2088,-2422,-2999,-3200,-3857), | ||
#' y = c(0,150,300,550,820,915,750,620,400,264)) | ||
#' | ||
#' df <- soccerNormXY(df, -5250, 5250, -3400, 3400, lengthPitch, widthPitch) | ||
#' | ||
#' soccerPath(df, lengthPitch = lengthPitch, widthPitch = widthPitch) | ||
#' | ||
#' @export | ||
soccerNormXY <- function(dat, xMin, xMax, yMin, yMax, pitchLength, pitchWidth) { | ||
|
||
dat$x <- (dat$x - xMin) / diff(c(xMin,xMax)) * pitchLength | ||
dat$y <- (dat$y - yMin) / diff(c(yMin,yMax)) * pitchWidth | ||
|
||
return(dat) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.