Search This Blog

Pass Map in football using R

In this article, it will be demonstrated how to utilize R in order to visualize football data. Upon completion of reading this article, the reader will be capable of creating visualizations like the one presented.

Open data from StatsBomb, a football analytics company that provides event data and analytics services for football clubs, will be used in this article. A variety of leagues, such as the UEFA Champions League, the World Cup, and the Indian Super League, are included in the open data. Visualizations based on a match from the 2018/19 Champions League final between Tottenham Hotspur and Liverpool will be created in this article. The data, which is in a JSON file format, may be challenging to analyze, but can be retrieved easily using a function called StatsBombR.

This code is using several libraries, including tidyverse, StatsBombR, and ggsoccer, which are all packages that provide various functions and tools for data manipulation, visualization, and analysis.

The first line of code retrieves a list of all available competitions from StatsBomb using the FreeCompetitions() function from the StatsBombR library. The next line filters this list to only include the competition with an ID of 16 and a season name of "2018/2019".

The third line of code retrieves a list of all available matches for the competition that was selected in the previous step using the FreeMatches() function and the ucl_final1819 object.

The fourth line of code retrieves the event data for the matches using the get.matchFree() function and the matches object. This event data is then cleaned and processed in the fifth line of code using the allclean() function from the ggsoccer library.

Finally, the sixth line of code filters the cleaned data to only include passes made by a player named Harry Kane using the filter() function from the tidyverse library. This filtered data is then stored in an object called Kane_pass.

After organizing the data its time to create the pass map. Here is the code:

This code is using the ggplot2 package from the tidyverse library to create a visualization of Harry Kane's passing map from a UEFA Champions League Final match that took place in the 2018/2019 season.

The ggplot() function is used to specify the data that will be plotted, which in this case is the Kane_pass data frame that was created in the previous code block.

The annotate_pitch() function is used to draw the soccer pitch on the plot, using dimensions provided by the pitch_statsbomb object and using the specified fill and color values.

The geom_segment() function is used to draw lines between the locations where the passes started and ended, using the location x and location y columns for the starting locations and the pass.end_location.x and pass.end_location.y columns for the ending locations. The arrow() function is used to add arrows to the ends of the lines to indicate the direction of the passes.

The labs() function is used to add titles, subtitles, and a caption to the plot, and the theme() function is used to customize the appearance of the plot, including the background color, font family and color, and the visibility of certain elements such as the grid and axis labels.

Running the codes above will yield the following pass map:

Credit: Irfan Alghani Khalid Published in Towards Data Science

0 Comments:

Post a Comment