JoGall / soccermatics

Tools for visualisation and analysis of soccer tracking and event data
308 stars 36 forks source link

unable to find "location.x" and "location.y" for plotting #14

Closed Ryo-N7 closed 5 years ago

Ryo-N7 commented 5 years ago

hey Joe!

been trying to use your recently updated package. I've been trying to replicate the cool graphs you showed with the sample data (Griezmann and France shot/pass maps) with other world cup matches but I keep running into a problem regarding the location data necessary to pass onto the graph functions like soccerShotmap(), soccerPassmap(), etc.

The matches I tried were Japan vs. Senegal, Japan vs. Belgium, and Brazil vs. Costa Rica.

For example:

WC_Matches <- FreeMatches(43)

JPN_Matches <- WC_Matches %>% filter(home_team.home_team_id == 778 | away_team.away_team_id == 778)

jp_sen <- get.matchFree(JPN_Matches[1, ])

jp_sen %>% 
  filter(player.name == "Takashi Inui") %>% 
  soccerShotmap(theme = "grey")

then I would get the error: Error in FUN(X[[i]], ...) : object 'location.y' not found

I tried to separate the coordinates found in the "location" column like so:

jp_sen %>% 
  filter(player.name == "Takashi Inui") %>% 
  separate(location, into = c("x", "y"), sep = ",") %>% 
  mutate(location.y = x %>% as.factor() %>% as.numeric(),
         location.x = y %>% as.factor() %>% as.numeric()) %>% 
  soccerShotmap(theme = "grey")

but I get a empty map with the text data but no points on the field.

Here's another example with the soccerPassmap() function:

# not work
jp_sen %>% 
  filter(team.name == "Japan") %>% 
  soccerPassmap(fill = "lightblue", arrow = "r", theme = "light")
# Error in `[.data.frame`(df, , x) : undefined columns selected

# works but wrong positioning
jp_sen %>% 
  filter(team.name == "Japan") %>% 
  separate(location, into = c("x", "y"), sep = ",") %>% 
  mutate(location.x = x %>% as.factor() %>% as.numeric(),
         location.y = y %>% as.factor() %>% as.numeric()) %>% 
  soccerPassmap(fill = "lightblue", arrow = "r", theme = "light")

jpnpassmapexample

Is there anyway you could tell me where/what that "location.x" and "location.y" data columns are supposed to be? Just checking the example data set, the coordinates in "location.x" and "location.y" seem to be very different from the coordinates in "location" so maybe I'm missing something in processing it?

Sorry for the long post, any help would be appreciated!

JoGall commented 5 years ago

Hi Ryo,

The issue here is needing to process the raw data retrieved by get.matchFree() using other StatsBombR functions like cleanlocations(). I made a helper function in a fork of the package here to automate this process if you're interested.

Once you have the cleaned data containing variables location.x and location.y, you need to use the soccerTransform() function with option method = "statsbomb" to convert the data from StatsBomb coordinates (1 <= x <= 120, 1 <= y <= 80) to metre units for plotting with soccermatics (by default this function uses 105m x 68m for pitch length and width, as do other soccermatics functions, but you can specify alternative pitch dimensions).

Code should look something like this then:

library(dplyr)

# get Japan matches
WC_Matches <- StatsBombR::FreeMatches(43)
JPN_Matches <- WC_Matches %>% 
  filter(home_team.home_team_id == 778 | away_team.away_team_id == 778)

# get all StatsBomb data
jp <- StatsBombR::allinfo(JPN_Matches)

# transform x,y-coords to real-world units for compatability with soccermatics
jp <- jp %>% 
  soccermatics::soccerTransform(method = "statsbomb")

# shotmap for multiple matches
jp %>% 
  filter(player.name == "Takashi Inui") %>% 
  soccermatics::soccerShotmap(theme = "grey",
                              title = "Takashi Inui",
                              subtitle = "World Cup 2018")

rplot08

# passmap for one match
jp_sen <- jp %>% 
  filter(match_id == 7556)

jp_sen %>% 
  filter(team.name == "Japan") %>% 
  soccermatics::soccerPassmap(fill = "lightblue", arrow = "r", theme = "light",
                              title = "Japan (vs. Senegal, World Cup 2018)")

rplot10

Hope that helps!

Ryo-N7 commented 5 years ago

ah I was looking at the soccerTransform() function but didn't think to look at anything in StatsBombR...

Thank you very much!