Skip to content

Latest commit

 

History

History
83 lines (56 loc) · 3.6 KB

File metadata and controls

83 lines (56 loc) · 3.6 KB

Mapping Recieves on an Football Pitch with ggplot2

For this activity we are going to use the pitch plot function from the last tutorial. We could copy and paste the code into this file, this would work but our new script would be a bit messy. A better way to achieve this is to put the function script in a different file and the load it.

I have saved the function in a file called PitchCreate.r and stored it in my working directory. It's easy to load:

source("PitchCreate.R")
options(warn=-1) # this turns warnings off, don't get into a habit of doing this
require(ggplot2)
## Loading required package: ggplot2

Now it's loaded lets feed the function some variables to get the base of our plot:

grass_colour <- "#202020"
line_colour <- "#797876"
background_colour <- "#202020"
goal_colour <- "#131313"
jp_colour <- "#F76065"
jp_include <- TRUE
ymax <- 7040
xmax <- 10600
jp_alpha <- 0.1

pitchBase <- createPitchJP(xmax, ymax, grass_colour, line_colour, background_colour, goal_colour, jp_colour, jp_include, jp_alpha)

pitchBase

Our base pitch map is ready! We are going to map the locations of every time Mesut Ozil recevies the ball. We will do this by producing some fake data as we don't have access to OPTA data. Let's say he recevied the ball 50 times in a match, we use the knowledge we developed in our random number and expected goals tutorial:

## let's create a function to generate some fake locations 
x <- runif(50,0,100)
y <- runif(50,0,100)

# now we have the OPTA style x,y which is displayed as a % of the x and a % of the y of a pitch. But we want to convert these to use on our pitch.
x2 <- (x/100) * xmax
y2 <- (y/100) * ymax

Now let's plot the touches using geom_point(). We need to feed it the x2 and y2 data and then tell it which colour the dots should be:

pitchBase + geom_point(aes(x=x2,y=y2),colour = "blue")

Ozil is attacking from left to right. Let's try and make the fake data more realistic for his position. We do this by generating a random number centered around a different mean and adjusting the standard deviation.

First we setup a function that takes the variables: n - number of numbers you want to generate mean - the mean of the probabilty distribution sd - standard deviation of the sample

Below we create 50 numbers for our x values, we put a mean of 70 which means our locations are more likely to be in the opposition half. Our y mean at 60 to give it a slight bias to being on the left of the pitch. The standard deviation we set at 25.

Let's see the results

## 

give_a_bias_number <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }

x <- give_a_bias_number(50,70,25)
y <- give_a_bias_number(50,60,25)

# now we have the OPTA style x,y which is displayed as a % of the x and a % of the y of a pitch. But we want to convert these to use on our pitch.
x2 <- (x/100) * xmax
y2 <- (y/100) * ymax

pitchBase + geom_point(aes(x=x2,y=y2),colour = "blue")

This is much more realistic! Try playing with the variables of our give_a_bias_number() function. Play with the variables to try and create realistic receiving coorindates for other positions... like right full-back.