In this R Markdown document, we’ll walk through the quick and easy steps to visualise Bundesliga 22/23 shooting data.
We start by loading the required libraries, including tidyverse for data manipulation and worldfootballR for accessing football match data.
library(worldfootballR)
## Warning: package 'worldfootballR' was built under R version 4.2.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
We load the Bundesliga 22-23 data, specifying the country, gender, tier, and season-end year.
Bundesliga_Team_Shooting_Data <- fb_season_team_stats(country = "GER", gender = "M", tier = "1st", season_end_year = 2023, stat_type = "shooting")
We filter the newly created data frame to feature only “team”.
Bundesliga_Team_Shooting_Data <- Bundesliga_Team_Shooting_Data %>%
filter(Team_or_Opponent == 'team')
We can now visualise a basic graph to display this season’s shots and shots on target for each team.
ggplot(data = Bundesliga_Team_Shooting_Data, aes(x = Sh_Standard, y = SoT_Standard )) +
geom_point( data = Bundesliga_Team_Shooting_Data,
aes( x = Sh_Standard, y = SoT_Standard)) +
facet_wrap(~ Squad)
If you would like to take an extra step to display more key information as a plot, we can manipulate the size of the points within the plot to reveal the expected goals. The larger the point, the more likely the team was to score goals throughout the season.
ggplot(data = Bundesliga_Team_Shooting_Data, aes(x = Sh_Standard, y = SoT_Standard )) +
geom_point(data = Bundesliga_Team_Shooting_Data,
aes(x = Sh_Standard, y = SoT_Standard, color = Squad, size = xG_Expected)) +
theme(legend.position = "right")