ggsoccer library to plot shots and passes

rstats

Lets Create this data viz for shots

shots

Code

# install.packages("devtools")
# devtools::install_github("torvaney/ggsoccer")
# I have to install manually rlang 

# Usage
# This example uses ggsoccer to plot a set of passes onto a soccer pitch.

library(ggplot2)
library(ggsoccer)

pass_data <- data.frame(x = c(24, 18, 64, 78, 53),
                        y = c(43, 55, 88, 18, 44),
                        x2 = c(34, 44, 81, 85, 64),
                        y2 = c(40, 62, 89, 44, 28))


ggplot(pass_data) +
  annotate_pitch() +
  geom_segment(aes(x = x, y = y, xend = x2, yend = y2),
   arrow = arrow(length = unit(0.25, "cm"),type = "closed")) +
  theme_pitch() +
  direction_label() +
  ggtitle("Passmap", "ggsoccer example")


# Because ggsoccer is implemented as ggplot layers, it makes customising a plot easy.
# Here is a different example, plotting shots on a **gray** pitch.

# Note that by default, ggsoccer will display the whole pitch. To display a
# subsection of the pitch, simply set the plot limits as you would with any other
# ggplot2 plot. Here, we use the `xlim` and `ylim` arguments to `coord_flip`:


#  SHOTS

shots <- data.frame(x = c(90, 85, 82, 78, 83, 74),
                    y = c(43, 40, 52, 56, 44, 71))

ggplot(shots) +
  annotate_pitch(colour = "gray70", fill = "gray90") +
  geom_point(aes(x = x, y = y),  fill = "white", size = 4,  pch = 21) +
  theme_pitch()+
  coord_flip(xlim = c(49, 101), ylim = c(-1, 101))+
  ggtitle("Shotmap", "ggsoccer shots")


### StatsBomb data

# Finally, different data providers may use alternative co-ordinate systems to ggsoccer default 100x100.
#  For instance,[StatsBomb's release of free data]() uses a 120x80 co-ordinate system.
#  This can be easily handled with the `*_scale` arguments to `annotate_pitch`:


# Rescale shots to use StatsBomb-style coordinates

shots_rescaled <- data.frame(x = shots$x * 1.20,  y = shots$y * 0.80)

ggplot(shots_rescaled) +
  annotate_pitch(x_scale = 1.2,  y_scale = 0.8, colour = "white",  fill = "green4") +
  geom_point(aes(x = x, y = y), fill = "yellow2", size = 5, pch = 21) +
  theme_pitch() +
  coord_flip(xlim = c(59, 121),   ylim = c(-1, 81)) +
  ggtitle("Shotmap", "ggsoccer example (120x80 co-ordinates)")


Other options

There are other packages that offer alternative pitch plotting options. Depending on your use case, you may want to check these out too:

Boxes

You can add notification, warning and error boxes like this:

Notification

Note: This is a notification box.

Warning

Warning: This is a warning box.

Error

Error: This is an error box.