knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-"
)
options(tibble.print_min = 5, tibble.print_max = 5)

scoutr

Lifecycle: experimental R build status

Overview

A complete and consistent set of functions for reading, manipulating, and visualizing Wyscout soccer data in R.

All public Wyscout data is available at https://figshare.com/collections/Soccer_match_event_dataset/4415000/2

Installation

The package is not available on CRAN, please install the development version.

# install.packages("devtools")
devtools::install_github("shawnsanto/scoutr")

Usage

Data manipulation

library(scoutr)
library(dplyr)
library(ggplot2)

# read and preview some event data
path <- system.file("extdata", "events_england.json", package = "scoutr")
events <- fc_read_events(path)

events %>%
  select(event_sec:end_y)

# transform pitch locations and create sf object
events %>%
  select(event_sec:end_y) %>%
  fc_locations_transform(x = c("start_x", "end_x"), 
                         y = c("start_y", "end_y"),
                         dim = c(105, 70), units = "meters") %>% 
  fc_locations_link(start_loc = c("start_x", "start_y"), 
                    end_loc   = c("end_x", "end_y"))

# define possessions
events %>% 
  select(match_id, event_name, team_id) %>% 
  fc_sequence_possession(event_var = "event_name", team_var = "team_id") %>% 
  print(n = 20)

# compute velocities
events %>%
  select(-starts_with("tag_")) %>%
  fc_locations_transform(x = c("start_x", "end_x"), y = c("start_y", "end_y")) %>%
  fc_sequence_possession(event_var = "event_name", team_var = "team_id") %>%
  fc_velocity_event(start_loc = c("start_x", "start_y"), end_loc = c("end_x", "end_y")) %>%
  fc_locations_link(start_loc = c("start_x", "start_y"), end_loc = c("end_x", "end_y")) %>% 
  fc_velocity_polygon(metric = "east_west_velocity", shape = "square",
                      fcn = "median", na.rm = TRUE, size = 5)

Data visualization

events %>% 
  fc_locations_transform(x = c("start_x", "end_x"), 
                         y = c("start_y", "end_y"),
                         dim = c(105, 70), units = "meters") %>% 
  filter(event_name == "Pass") %>% 
  ggplot(mapping = aes(x = start_x, y = start_y)) +
  fc_annotate_pitch(palette = "classic") +
  geom_point(color = "grey70") +
  fc_theme_classic()


events %>% 
  filter(event_name %in% c("Pass", "Shot")) %>% 
  ggplot(mapping = aes(start_x, start_y)) +
  fc_annotate_pitch(dimensions = c(100, 100), palette = "bw", coord_flip = TRUE) +
  geom_point(aes(color = event_name), size = 2) +
  scale_color_manual(values = c("grey70", "red")) +
  labs(color = "Event") +
  coord_flip() +
  fc_theme_bw() +
  theme(legend.position = "bottom", aspect.ratio = 105 / 70)


events %>% 
  filter(event_name == "Shot") %>% 
  ggplot(mapping = aes(start_x, start_y)) +
  fc_annotate_pitch(dimensions = c(100, 100)) +
  fc_annotate_arrow(x = 50, y = -10) +
  geom_point(aes(color = team_id), size = 3) +
  labs(color = "Team ID", title = "Shots on goal") +
  fc_theme_gw() +
  theme(legend.position = "bottom")


Themes

library(patchwork)
p1 <- ggplot() + fc_annotate_pitch(palette = "bw") + 
  labs(title = "  Black-white: fc_theme_bw()") + fc_theme_bw()
p2 <- ggplot() + fc_annotate_pitch(palette = "classic") + 
  labs(title = "  Classic: fc_theme_classic()") + fc_theme_classic() +
  theme(plot.title = element_text(colour = "white"))
p3 <- ggplot() + fc_annotate_pitch(palette = "dark") + 
  labs(title = "  Dark: fc_theme_dark()") + fc_theme_dark() +
  theme(plot.title = element_text(colour = "white"))
p4 <- ggplot() + fc_annotate_pitch(palette = "gw") + 
  labs(title = "  Grey-white: fc_theme_gw()") + fc_theme_gw()
p5 <- ggplot() + fc_annotate_pitch(palette = "smurf") + 
  labs(title = "  Smurf: fc_theme_smurf()") + fc_theme_smurf() +
  theme(plot.title = element_text(colour = "white"))
p6 <- ggplot() + fc_annotate_pitch(palette = "wc") + 
  labs(title = "  World Cup: fc_theme_wc()") + fc_theme_wc() +
  theme(plot.title = element_text(colour = "white"))

p1 + p2 + p3 + p4 + p5 + p6 +
  plot_layout(ncol = 2)

References

Pappalardo, L., Cintia, P., Rossi, A. et al. A public data set of spatio-temporal match events in soccer competitions. Sci Data 6, 236 (2019). https://doi.org/10.1038/s41597-019-0247-7



shawnsanto/scoutr documentation built on Feb. 27, 2021, 1:02 p.m.