Using Optical Tracking Data from Second Spectrum to Visualize Long Goal-scoring Build-ups

Photo by Nathy dog on Unsplash.

Two weeks ago, Nashville SC scored a lovely team goal. All 10 field players contributed to a 15-pass sequence, putting Nashville up 2-1 over D.C. United.

Thanks to Major League Soccer and Second Spectrum, some of us lucky folks at American Soccer Analysis have access to 2D tracking data for every regular season and playoff MLS game. Just by plotting each frame of these special goal-scoring sequences, it’s enlightening to see how these attacking teams move and exploit space to ultimately unlock the opposing defense.

Because I used {gridExtra} to arrange each component of the visualizations, I’m constructing and exporting each individual frame as a still image. To speed things up, I took advantage of {doParallel} and {foreach} to distribute the workload. And in order for each process’s {ggsave} call to be able to write to the same location, I had to set type = "cairo-png".

Finally, to stitch all the frames together into an MP4 file, I’m leveraging the {mapmate} wrapper around the FFmpeg command line tool:

mapmate::ffmpeg(
    dir = tile_save_path, 
    pattern = glue::glue("{save_file_prefix}_%06d.png"),
    output_dir = gif_mp4_save_path, 
    output = glue::glue("{save_file_prefix}.mp4"),
    rate = export_fps, 
    start = start_frame, 
    overwrite = TRUE
)

This content was originally developed as part of a Twitter thread, and each original tweet is embedded below. The videos are best viewed on desktop.

August 15, 2021  |  Nashville SC (2) - 1 D.C. United 

August 21, 2021  |  Columbus Crew 1 - (2) Seattle Sounders FC 

July 25, 2021  |  Inter Miami CF 1 - (1) Philadelphia Union 

July 17, 2021  |  Toronto FC (1) - 0 Orlando City SC 

July 3, 2021  |  CF Montréal (1) - 0 Inter Miami CF 

June 19, 2021  |  FC Cincinnati 0 - (2) Colorado Rapids 

May 2, 2021  |  Seattle Sounders FC (2) - 0 LA Galaxy 

November 22, 2020  |  Minnesota United FC (3) - 0 Colorado Rapids 

November 1, 2020  |  Colorado Rapids (1) - 0 Seattle Sounders FC 

October 14, 2020  |  Vancouver Whitecaps FC (1) - 0 Los Angeles FC 

September 23, 2020  |  Real Salt Lake (2) - 0 LA Galaxy 

August 26, 2020  |  Orlando City SC (1) - 1 Nashville SC 

Data: Second Spectrum