-
Notifications
You must be signed in to change notification settings - Fork 0
/
GROUP 6 FINAL ASSIGNMENT.Rmd
1120 lines (834 loc) · 49.3 KB
/
GROUP 6 FINAL ASSIGNMENT.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "group project"
output: html_document
date: "2023-11-20"
---
# Where should a striker go to become world class?
## 1) Introduction
At Star-In-Making, we are committed to harnessing the power of data analytics to guide emerging strikers in selecting the optimal team to commence their professional careers. We recognize the significance of the initial step in shaping their global brand and leading the scoring charts in competitive leagues. Understanding the nuances of European football's competitive environment, we emphasize the importance of a well-suited match between a player's attributes and a league's characteristics. For instance, a league with a reputation for physicality may not be the ideal setting for a player with less physical prowess.
To address this, Star-In-Making has developed a methodological approach based on historical data analysis to assist strikers in making well-informed decisions. Our methodology encompasses a three-fold strategy:
1. Identifying the most suitable league for a striker's skill set and playing style.
2. Determining the teams that provide the best opportunities for goal scoring.
3. Analyzing potential team members whom they might effectively replace.
While our approach takes into consideration a multitude of variables, it adheres to the Pareto Principle (80/20 rule), ensuring that it serves as an efficient and pragmatic first step in the decision-making process. Our overarching goal is to lay the foundation for nurturing future Ballon d'Or contenders, solidifying our commitment to fostering exceptional talent in the realm of international football.
## 2) Analysis
### a) Inspect, Clean, Explore
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, warning=FALSE, eval = FALSE}
install.packages('ggsoccer')
install.packages("patchwork")
install.packages('devtools')
devtools::install_github("statsbomb/SDMTools")
devtools::install_github("statsbomb/StatsBombR")
```
```{r, eval = FALSE}
library(tidyverse)
library(lubridate)
library(ggrepel)
library(gridExtra)
library(janitor)
library(skimr)
library(hrbrthemes)
library(ggplot2)
library(vroom)
library(readr)
library(forcats) # Load forcats for fct_reorder function
library(ggsoccer)
library(patchwork)
library(tidyverse)
library(StatsBombR)
library(ggsoccer)
```
```{r load datasets, warning=FALSE, message=FALSE, eval = FALSE}
# URL of the zip file
zip_url <- "https://github.com/alankritatewari/data_viz_group6/raw/main/data.zip"
# Filepath where you want to save the downloaded zip file (current directory)
zip_filepath <- file.path(getwd(), "data.zip")
# Download the zip file
download.file(zip_url, destfile = zip_filepath, mode = "wb")
# Unzip the contents into the current directory
unzip(zip_filepath, exdir = getwd())
# Read the CSV files into R
appearances <- read_csv("appearances.csv")
players <- read_csv("players.csv")
teams <- read_csv("teams.csv")
teamstats <- read_csv("teamstats.csv")
shots <- read_csv("shots.csv")
leagues <- read_csv("leagues.csv")
games <- read_csv("games.csv")
```
Star-In-Making's analytical framework is underpinned by a comprehensive database comprising seven distinct but interconnected data sets. These datasets are meticulously linked through key identifiers: League ID, Player ID, and Game ID. This extensive repository encompasses detailed match-level data from five major European leagues, spanning a period from 2015 to 2021.
Our approach is not only retrospective but also forward-looking. By leveraging historical data, we aim to validate our recommendations against the current European standings and the real-time performance of the players we suggest as potential replacements. This method ensures a robust, data-driven foundation for our recommendations, enabling us to provide strategic insights with a high degree of accuracy and relevance to the contemporary football landscape.
### b) What League should the striker join?
In addressing the pivotal question of determining the most suitable league for a striker to join, Star-In-Making employs a strategic approach grounded in our extensive experience and understanding of the variances in competitiveness and playing styles across different leagues. Our objective is to align the striker with a league that offers an ecosystem conducive to their flourishing and development.
Our analysis delves into key metrics that are critical in evaluating the compatibility between a player's skills and a league's characteristics. These metrics include:
1. **Average Goals Scored Per Game**: This indicator provides insight into the offensive nature of the league and the potential opportunities for a striker to score.
2. **Fouls Per Game (Including Yellow and Red Cards)**: This measure sheds light on the physicality and disciplinary aspects of the league, which are crucial for understanding the level of physical challenge a player might face.
3. **Distribution of Goal Scoring Percentage Across the League**: This analysis helps in understanding how goals are spread among teams, indicating whether a league is dominated by a few teams or if scoring opportunities are more evenly distributed.
```{r inspect data, warning =FALSE, message = FALSE, eval = FALSE}
skim(appearances)
skim(players)
skim(teams)
skim(teamstats)
skim(shots)
skim(leagues)
skim(games)
```
```{r warning=FALSE, message=FALSE}
#Create two columns showing the total amount of home and away goals per game
goals_per_game <-
games %>%
group_by(leagueID, season, gameID) %>%
summarise(away_g = sum(awayGoals),
home_g = sum(homeGoals))
# Create a two columns showing the total amount of home and away goals per league
goals <-
goals_per_game %>%
group_by(gameID, leagueID) %>%
summarise(away_g = sum(away_g),
home_g = sum(home_g),
count=n()) %>%
# Use ungroup so we can group by league and summarise
ungroup() %>%
group_by(leagueID) %>%
summarise(away_g = sum(away_g),
home_g = sum(home_g),
games = sum(count),
avg_away = (away_g/(games/2)),
avg_home = (home_g/(games/2)))
# Pivot Longer to make the dataset ready to plot.
goals_longer<-
goals %>%
pivot_longer(cols = c(avg_away, avg_home), names_to = "type", values_to = "number")
```
```{r, warning =FALSE, message = FALSE}
# Match the league names
league_names <- c("1" = "Premier League", "2" = "Serie A", "3" = "Bundesliga", "4" = "La Liga", "5" = "Ligue 1")
# Set default colors for home and away
home_color <- "#515151"
away_color <- "gray"
# Specific colors for Bundesliga
bundesliga_home_color <- "#D3010C" # blue
bundesliga_away_color <- "#e06f6f" # orange
# Create a unique identifier for each bar based on leagueID and type
goals_longer <- goals_longer %>%
mutate(color_id =ifelse(leagueID == 3,
paste(leagueID, type, sep = "_"),
paste("other", type, sep = "_")) )
# Create a named vector of colors for each type
color_values <- setNames(
c(away_color, home_color, bundesliga_away_color, bundesliga_home_color),
c("other_avg_away", "other_avg_home", "3_avg_away", "3_avg_home"))
# Create the labels for the legend
labels_for_legend <- c("Bundesliga Away Goals", "Bundesliga Home Goals", "Away Goals", "Home Goals") # Modify these as you like
# Plot
plot_goals <-
goals_longer %>%
ggplot(aes(leagueID, number, fill=color_id))+
geom_col(position = "dodge")+
geom_text(aes(label = round(number,2), group = color_id),
position = position_dodge(width = 0.9),
vjust = -0.3, size = 3) +
scale_fill_manual(values = color_values,
labels = labels_for_legend,
name = "") +
scale_x_continuous(breaks =
as.numeric(names(league_names)), labels = league_names)+
labs(x = "", y = "Average Goals per Game",
title = "The Bundesliga Stands Out:
Leading in Average Goals per Game",
subtitle = "Home vs. Away: A Comparison of Average Goals Across Leagues")+
theme_minimal() +
theme(legend.title = element_blank(),
plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
axis.text.y=element_blank())
# Print the plot
plot_goals
```
The analysis of our data sets has yielded a compelling insight, particularly regarding the Bundesliga's standing as a highly favorable environment for strikers. The graph we have compiled presents a clear picture: the Bundesliga leads in terms of average home and away goals scored per game. This trend positions the Bundesliga as an ideal destination for strikers looking to maximize their goal-scoring potential.
Given that the number of games played across the various leagues shows no significant statistical variance, we deduce that the Bundesliga's higher goal-scoring rate can be attributed to one of two likely factors:
1. **Relative Competitiveness of the League**: It's possible that the Bundesliga might exhibit a lower degree of competitiveness compared to its counterparts, resulting in certain teams scoring a disproportionately high number of goals.
2. **Offensive Playing Style**: Alternatively, the league may inherently prioritize an attacking style of play, naturally leading to more goal-scoring opportunities.
In either case, these factors significantly contribute to the Bundesliga's appeal as a strikingly favorable league for goal-scorers.
```{r, warning =FALSE, message = FALSE}
# Join tables
data <- teamstats %>%
left_join(games, by = "gameID")
```
```{r plots dangerous, warning= FALSE, message = FALSE}
# Plot Yellow cards
data %>%
# select columns
select(leagueID, fouls, yellowCards) %>%
# drop NAs
drop_na(yellowCards) %>%
group_by(leagueID) %>%
# Find avg # of cards
summarise(yellowCards = mean(yellowCards)) %>%
# Join league names
left_join(leagues, by = "leagueID") %>%
# Column to color
mutate(highlight = ifelse(leagueID %in% c(2,4), 1, 0)) %>%
# Plot
ggplot(aes(x = fct_reorder(name, leagueID), y = yellowCards, fill = factor(highlight))) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#bebebe', '#c70007')) +
# Custom titles and axes
labs(title = "Serie A and La Liga have more yellow cards",
subtitle = "Average number of yellow cards per game",
x = "",
y = ""
#y = "Yellow cards per game"
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
# Plot Red cards
data %>%
# select columns
select(leagueID, fouls, redCards) %>%
# drop NAs
drop_na(redCards) %>%
group_by(leagueID) %>%
# Find avg # of cards
summarise(redCards = mean(redCards)) %>%
# Join league names
left_join(leagues, by = "leagueID") %>%
# Column to color
mutate(highlight = ifelse(leagueID %in% c(2, 5), 1, 0)) %>%
# Plot
ggplot(aes(x = fct_reorder(name, leagueID), y = redCards, fill = factor(highlight))) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#bebebe', '#c70007')) +
# Custom titles and axes
labs(title = "Serie A and Ligue 1 have more red cards",
subtitle = "Average number of red cards per game",
x = "",
y = ""
#y = "Red cards per game"
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
#####
# Plot Fouls
data %>%
# select columns
select(leagueID, fouls) %>%
group_by(leagueID) %>%
# Find avg # of fouls
summarise(fouls = mean(fouls)) %>%
# Join league names
left_join(leagues, by = "leagueID") %>%
# Column to color
mutate(highlight = ifelse(leagueID %in% c(2,4), 1, 0)) %>%
# Plot
ggplot(aes(x = fct_reorder(name, leagueID), y = fouls, fill = factor(highlight))) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#bebebe', '#c70007')) +
# Custom titles and axes
labs(title = "Serie A and La Liga are more 'dangerous'",
subtitle = "Average number of fouls per game",
x = "",
y = ""
#y = "Fouls cards per game"
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
```
Our comprehensive analysis, supported by graphically represented data, reveals significant insights into the disciplinary aspects of various leagues, with a particular focus on Serie A, La Liga, and Ligue 1. The findings from our study lead to several key conclusions:
1. **Ligue 1's Disciplinary Concerns**: The data indicates that Ligue 1 experiences a notably high frequency of red cards. This suggests that the nature of fouls in this league tends to be more severe, potentially leading to direct red card offenses. Such a trend raises concerns about the harshness of challenges faced by players in Ligue 1.
2. **Serie A and La Liga's Risk of Injury**: Both Serie A and La Liga are characterized by a higher number of fouls and yellow cards. This increased rate of fouls correlates with a heightened risk of injury for players, as frequent physical challenges can lead to more frequent and potentially serious injuries.
3. **Bundesliga as the Preferred Choice**: Integrating this analysis with our earlier conclusions about goal-scoring opportunities, the Bundesliga emerges as a particularly attractive option for strikers. Not only does it offer a higher average of goals scored per game, but it also appears to have a lower risk of severe fouls and injuries compared to Serie A, La Liga, and Ligue 1. This combination of factors makes the Bundesliga an ideal league for strikers to commence their careers, offering a balance of high goal-scoring potential and a relatively safer playing environment.
```{r, warning =FALSE, message = FALSE}
# Merge datasets
m_data <- merge(teamstats, games, by = "gameID", all.x = TRUE)
merge_data <- merge(m_data, leagues, by = "leagueID", all.x = TRUE)
merged_data <- merge(merge_data, teams, by = "teamID", all.x = TRUE)
```
```{r, warning =FALSE, message = FALSE}
# Calculate the total goals scored by each team in one league
total_goals_by_team <- merged_data %>%
filter(leagueID %in% c(1, 3, 5)) %>%
group_by(name.y, name.x) %>%
summarise(total_goals = sum(goals))
# Calculate the percentage of goals scored by each team in one league
percentage_goals_by_team <- total_goals_by_team %>%
group_by(name.x) %>%
mutate(percentage_goals = (total_goals / sum(total_goals)) * 100)
# Convert teamID to character
unique_merged_data_league <- percentage_goals_by_team %>%
mutate(name.y = as.factor(name.y))
# Reorder teamID levels within each league in descending order
unique_merged_data_league <- unique_merged_data_league %>%
group_by(name.x) %>%
mutate(name.y = fct_reorder(name.y, percentage_goals, .desc = TRUE))
# Get the top 5 teams in each league with the highest share of goals scored
top_5_teams <- unique_merged_data_league %>%
group_by(name.x) %>%
slice_max(order_by = percentage_goals, n = 5) %>%
mutate(name.y = fct_reorder(name.y, percentage_goals, .desc = TRUE)) %>%
select(percentage_goals, name.y, name.x)
# Create a bar plot using ggplot2 without legend and with text labels
ggplot(top_5_teams, aes(x = percentage_goals, y = fct_reorder(name.y, percentage_goals), fill = as.factor(name.x))) +
geom_bar(stat = "identity") +
geom_text(aes(label = sprintf("%.2f%%", percentage_goals), group = name.x), position = position_stack(vjust = 0.5), size = 3, color = "white") +
scale_fill_manual(values = c("Premier League" = "#38003c", "Bundesliga" = "#D3010C", "Ligue 1" = "#dae025")) +
# Set specific league colors
facet_wrap(~ name.x, scales = "free_y") +
labs(title = "Top 5 Teams with Highest Share of
Goals Scored in Each League",
subtitle = "Bayern Munich and Borussia Dortmund collectively score 17.92% goals for
Bundesliga, making it a top choice",
x = "Percentage of Goals Scored (Within League)", y = "") +
scale_x_continuous() +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 18, hjust = 0), # Left align title
plot.subtitle = element_text(hjust = 0), # Left align subtitle
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank(),
legend.position = "none"
)
```
The analytical insights derived from our graph not only reinforce the Bundesliga as the league of choice but also pinpoint two prominent teams, Bayern Munich and Borussia Dortmund, as key players in the league's goal-scoring dynamics. This detailed analysis leads us to a decisive conclusion regarding the Bundesliga as the optimal league for a burgeoning striker.
Our findings clearly illustrate that Bayern Munich and Borussia Dortmund contribute significantly to the Bundesliga's high goal tally. This dominance in goal scoring presents two distinct opportunities for a striker:
1. **Joining Bayern Munich**: Aligning with Bayern Munich offers the striker a chance to be part of an already prolific goal-scoring setup. The team's track record and established success in finding the back of the net provide an ideal platform for a striker to flourish and contribute to the team's ongoing dominance.
2. **Supporting Borussia Dortmund**: Alternatively, joining Borussia Dortmund presents a unique opportunity to play a crucial role in bridging the gap to Bayern Munich. This path involves aiding Dortmund in enhancing their offensive capabilities to compete more closely with Bayern Munich's scoring prowess.
### c) What team should he join?
Determining the most suitable team for a striker is a multifaceted and intricate endeavor, influenced by a myriad of factors, including managerial tactics, transfer dynamics, financial capabilities, and other less tangible elements. Nevertheless, adhering to the Pareto Principle (80/20 rule), our focus will be streamlined to concentrate on key, impactful metrics that offer significant insights. Our analysis includes:
1. **Key Passes / Assist Analysis**: Teams exhibiting a lower conversion rate of key passes into assists present an opportunity for a striker. Key passes represent goal-scoring opportunities that have not been capitalized on, indicating potential for a striker to enhance the team's effectiveness in front of the goal.
2. **Examination of Set Pieces and Offensive Metrics**: We will delve into the frequency of corners, the average number of shots taken, and the rate of completed passes. These statistics provide an understanding of a team's offensive approach and the availability of scoring opportunities.
3. **Analysis of Each Team's Top Scorer Over the Years**: Reviewing the performance and strategies of the top scorers in each team across different seasons will shed light on the team's attacking playstyle and the role a striker is likely to assume.
4. **Heat Map for Ball Possession and Tactical Concentration**: A heat map analysis will enable us to visualize the areas of the pitch where teams concentrate their play, offering insights into their tactical inclinations and ball possession patterns.
```{r match teams with players, warning =FALSE, message = FALSE}
# keep only required teams
games1 <- games %>%
filter(leagueID == 3) %>%
filter(homeTeamID == 117 | homeTeamID == 129 | awayTeamID == 117 | awayTeamID == 129) %>%
select(gameID, homeTeamID, awayTeamID)
# reduce columns
appearances1 <- appearances %>%
select(gameID, playerID)
# join 2 tables
pairs <- games %>%
left_join(appearances, by = "gameID")
# create pairs: away + player and home + player
pairs <- pairs %>%
mutate(pl_matches = paste(homeTeamID, playerID, sep = "+"),
pl_matches2 = paste(awayTeamID, playerID, sep = "+"))
# calculate appearances
match_player <- pairs %>%
pivot_longer(cols = c(pl_matches, pl_matches2), names_to = "names", values_to = "matches") %>%
select(-names) %>%
count(matches) %>%
arrange(desc(n))
# select only most common cases
players1 <- match_player %>%
mutate(teamID = substring(matches, first = 1, last = 3),
playerID = substring(matches, first = 5, last = 7)) %>%
filter(teamID == 117 | teamID == 129) %>%
distinct(teamID, playerID)
```
```{r passes per team, warning =FALSE, message = FALSE}
# Creating a database which shows key passes per team
# Select only the relevant columns
appearances <- appearances %>%
select(playerID, gameID, keyPasses, assists, leagueID) %>%
mutate(playerID = as.factor(playerID))
# Match the player ID with the Team ID
pct_key_passes <-
left_join(appearances, players1, by = "playerID") %>%
na.omit()
# Create a Dataset with only game ID and Season
season <- games %>%
select(gameID, season)
# Match the season to the game ID
pct_key_passes <- left_join(pct_key_passes, season, by = "gameID")
# Summarise the assists and key passes per team
pct_key_passes <-
pct_key_passes %>%
group_by(teamID, season) %>%
summarise(key_passes = sum(keyPasses),
assists = sum(assists))
# Create a column with the ratio key passes : assists
pct_key_passes <-
pct_key_passes %>%
mutate(ratio = key_passes/assists)
```
```{r plot passes per team, warning =FALSE, message = FALSE}
# Create a named vector with the team IDs and corresponding names to use when facet wraping
# Create a named vector with the team IDs and corresponding names to use when facet wraping
team_names <- setNames(c("Bayern Munich", "Borussia Dortmund"), c("117", "129"))
# Define the colors for each team
team_colors <- c('117' = '#DC052D', '129' = '#FDE100')
plot_keyp <- ggplot(pct_key_passes, aes(x = season, y = ratio, group = teamID,
color = as.factor(teamID))) +
geom_line() +
geom_point()+
facet_wrap(~teamID, labeller = labeller(teamID = team_names)) +
scale_color_manual(values = team_colors) +
geom_text(aes(label = round(ratio, 2)),
vjust = -1,
hjust = 0.5,
color = "black",
size=2.5)+
labs(title = "Synced Trajectories: Bayern Munich and
Borussia Dortmund Match Playmaking Trends",
subtitle = "Key Passes to Assists Ratios Reveal Consistent Patterns from 2014 to 2020",
x= "",
y="") +
scale_y_continuous(limits = c(0,NA))+
theme_minimal()+
theme(legend.position = "none",
plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
#panel.grid.minor =element_blank()
)
plot_keyp
```
The graph we have analyzed reveals a noteworthy trend: both teams under consideration demonstrate parallel patterns across the observed seasons, with a remarkably similar ratio in the metrics being evaluated. This consistency in trend and the close alignment in their statistical ratios indicate an absence of significant differentiation between the two teams. However, we can look at other factors.
```{r opportunities, warning= FALSE, message = FALSE}
# Plot Corners
data %>%
# Filter teams
filter(teamID == 117 | teamID == 129) %>%
# select columns
select(corners, teamID) %>%
group_by(teamID) %>%
# Find avg # of fouls
summarise(corners = mean(corners)) %>%
# Join league names
left_join(teams, by = "teamID") %>%
# Plot
ggplot(aes(x = fct_reorder(name, teamID), y = corners, fill = name)) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#c70007', '#bebebe')) +
# Custom titles and axes
labs(title = "Bayern Munich creates more opportunities to score",
subtitle = "Average number of corners per game",
x = "",
y = ""
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
#####
# Plot Shots
data %>%
# Filter teams
filter(teamID == 117 | teamID == 129) %>%
# select columns
select(shots, teamID) %>%
group_by(teamID) %>%
# Find avg # of fouls
summarise(shots = mean(shots)) %>%
# Join league names
left_join(teams, by = "teamID") %>%
# Plot
ggplot(aes(x = fct_reorder(name, teamID), y = shots, fill = name)) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#c70007', '#bebebe')) +
# Custom titles and axes
labs(title = "Bayern Munich makes more shots",
subtitle = "Average number of shots per game",
x = "",
y = ""
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
#####
# Plot Completed Passes completed within an estimated distance of 20 yards of goal
data %>%
# Filter teams
filter(teamID == 117 | teamID == 129) %>%
# select columns
select(deep, teamID) %>%
group_by(teamID) %>%
# Find avg # of fouls
summarise(deep = mean(deep)) %>%
# Join league names
left_join(teams, by = "teamID") %>%
# Plot
ggplot(aes(x = fct_reorder(name, teamID), y = deep, fill = name)) +
geom_col() +
# Custom colors
scale_fill_manual(values=c('#c70007', '#bebebe')) +
# Custom titles and axes
labs(title = "Bayern Munich makes more completed passes",
subtitle = "Average number of completed passes per game",
x = "",
y = ""
) +
theme_minimal() +
# More customization
theme(plot.title = element_text(face="bold", size=18),
panel.grid.major = element_blank(),
panel.grid.minor =element_blank(),
legend.position = "")
```
Our analysis conclusively demonstrates that Bayern Munich consistently outperforms Borussia Dortmund across a range of key metrics, elucidating the reasons behind their sustained success in winning leagues and creating prolific scoring opportunities. This exceptional performance makes Bayern Munich an exemplary choice for any striker seeking a platform characterized by high goal-scoring potential and competitive excellence.
However, it's important to consider the qualitative aspects alongside these quantitative findings, particularly for a young, emerging striker. The likelihood of securing a spot in Bayern Munich's starting eleven might present a significant challenge, given the team's depth of established talent and competitive environment. In contrast, Borussia Dortmund has garnered a reputation for nurturing and developing young talent, offering a potentially more accessible pathway for a fledgling striker to gain valuable playing experience and grow professionally.
```{r, warning =FALSE, message = FALSE}
games<- games %>%
select(gameID, homeTeamID, awayTeamID, date) %>%
mutate(date = as.Date(date,format = "%Y-%m-%d")) %>%
mutate(year = year(date))
new_Data <- left_join(shots, games, by = "gameID")
top_scorer_dortmund <- left_join(new_Data, players, by = c("shooterID" = "playerID"))%>%
filter(homeTeamID == 129 | awayTeamID == 129) %>%
group_by(year, name) %>%
summarise(count = n(), .groups = "drop") %>%
group_by(year) %>%
top_n(1,count) %>%
rename(name_dortmund = name)
top_scorer_bayern <- left_join(new_Data, players, by = c("shooterID" = "playerID"))%>%
filter(homeTeamID == 117 | awayTeamID == 117) %>%
group_by(year, name) %>%
summarise(count = n(), .groups = "drop") %>%
group_by(year) %>%
top_n(1,count)%>%
rename(name_bayern = name)
top_scorer <- left_join(top_scorer_dortmund, top_scorer_bayern, by = "year")
custom_colors_dortmund <- c("Erling Haaland" = "#e34a33", "Marco Reus" = "#fdbb84", "Pierre-Emerick Aubameyang" = "#05205e")
plot_dortmund <- ggplot(top_scorer, aes(x = year, y = count.x, color = name_dortmund))+
geom_point()+
geom_line()+
geom_text(aes(label = count.x), vjust = 1.3)+
labs(y = "Number of Goals",
color = "Dortmund Player",
title = "Dortmund has more tactical diversity allowing
for different strikers to shine",
subtitle = "Top scorer of both clubs in a calendar year"
)+
theme_minimal() +
theme(
axis.title.x = element_blank(), # Remove x-axis title
axis.text.x = element_blank(), # Remove x-axis text/labels
axis.ticks.x = element_blank(), # Remove x-axis ticks
plot.title = element_text(hjust = 0, face="bold", size=18), # Left align title
plot.subtitle = element_text(hjust = 0), # Left align subtitle
panel.grid.major = element_blank()
) +
scale_color_manual(values = custom_colors_dortmund)
custom_colors_bayern <- c("Arjen Robben" = "#e34a33", "Robert Lewandowski" = "#fdbb84")
plot_bayern <- ggplot(top_scorer, aes(x = year, y = count.y, color = name_bayern))+
geom_point()+
geom_line()+
geom_text(aes(label = count.y), vjust = 1.3)+
labs(x = "",
y = "Number of Goals",
color = "Bayern Player")+
scale_color_manual(values = custom_colors_bayern) +
theme_minimal() +
theme(panel.grid.major = element_blank()) +
ylim(c(50,160))
plot_dortmund / plot_bayern
#Team Strategy is diverse
```
The graph we have examined provides compelling insights that align closely with our initial hypothesis, particularly regarding the strategic approaches of Borussia Dortmund and Bayern Munich. Our findings reveal that Borussia Dortmund fosters a diverse and inclusive strategy, allowing multiple strikers to thrive and contribute to the team's offensive efforts. This approach is indicative of a more varied and adaptable tactical framework, which is conducive to the growth and development of different strikers.
In contrast, Bayern Munich appears to adopt a star-striker strategy, focusing on maximizing the potential of a few key players in the offensive lineup. While this approach has undoubtedly contributed to their remarkable success, it implies a more centralized and possibly less varied attacking scheme.
This distinction holds significant implications, especially for a young striker looking to establish themselves. At Bayern Munich, the emphasis on a star-striker strategy might translate to limited playing time for an emerging talent, as the team tends to rely on their established top scorers. On the other hand, Borussia Dortmund's more diverse approach to goal scoring may offer greater opportunities for a young striker to gain playing experience and make a substantial impact.
Therefore, while the allure of joining a team like Bayern Munich is understandable, the strategic fit for a young striker might be more favorable at Borussia Dortmund, given their track record of nurturing diverse talent and providing a platform for various strikers to excel. This analysis is pivotal in guiding our decision-making process, ensuring that the chosen team aligns with the striker's need for growth and exposure in the early stages of their career.
The code for heatmap visualisation was taken from this [article:](https://towardsdatascience.com/how-to-visualize-football-data-using-r-ee963b3a0ba4)
```{r load data, warning =FALSE, message = FALSE}
# Retrieve all available competitions
Comp <- FreeCompetitions()
# Filter the competition
ucl_german <- Comp %>%
filter(competition_id==16 & season_name=="2012/2013")
#ucl_german <- Comp %>%
# filter(competition_id==16 & season_name=="2011/2012")
#ucl_german <- Comp %>%
# filter(competition_id==16 & season_name=="2009/2010")
# Retrieve all available matches
matches <- FreeMatches(ucl_german)
# Retrieve the event data
events_df <- get.matchFree(matches)
# Preprocess the data
clean_df <- allclean(events_df)
```
```{r prepare datasets, warning =FALSE, message = FALSE}
bayern_pressure <- clean_df %>%
filter(team.name == 'Bayern Munich') %>%
filter(type.name == 'Pressure')
borussia_pressure <- clean_df %>%
filter(team.name == 'Borussia Dortmund') %>%
filter(type.name == 'Pressure')
```
```{r plot heatmaps, warning= FALSE, message= FALSE}
p6 <- ggplot(bayern_pressure) +
# generate background as a football venue. LBS color + White
annotate_pitch(dimensions = pitch_statsbomb, fill='#05205e', colour='#ffffff') +
# generate heatmap based on X and Y pressure positions
geom_density2d_filled(aes(location.x, location.y, fill=..level..), alpha=0.4, contour_var= 'ndensity' ) +
# limit X and Y axes
scale_x_continuous(c(0, 120)) +
scale_y_continuous(c(0, 80)) +
# Add titles and caption
labs(title="Bayern Munich Pressure Heat Map",
subtitle="UEFA Champions League Final 12/13") +
# Make plot nicer
theme_minimal() +
# Change theme
theme(
#plot.background = element_rect(fill='#021e3f', color='#021e3f'),
#panel.background = element_rect(fill='#021e3f', color='#021e3f'),
# Adjust title and subtitle position
plot.title = element_text(hjust=0, face="bold", size=12),
plot.subtitle = element_text(hjust=0, size=8),
# Remove grid and axis ticks/ text
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
# Remove legend
legend.position = "none"
)
p7 <- ggplot(borussia_pressure) +
# generate background as a football venue. LBS color + White
annotate_pitch(dimensions = pitch_statsbomb, fill='#05205e', colour='#ffffff') +
# generate heatmap based on X and Y pressure positions
geom_density2d_filled(aes(location.x, location.y, fill=..level..), alpha=0.4, contour_var= 'ndensity' ) +
# limit X and Y axes
scale_x_continuous(c(0, 120)) +
scale_y_continuous(c(0, 80)) +
# Add titles and caption
labs(title="Borussia Dortmund Pressure Heat Map",
subtitle="UEFA Champions League Final 12/13",
caption="Data Source: StatsBomb") +
# Make plot nicer
theme_minimal() +
# Change theme
theme(
#plot.background = element_rect(fill='#021e3f', color='#021e3f'),
#panel.background = element_rect(fill='#021e3f', color='#021e3f'),
# Adjust title and subtitle position
plot.title = element_text(hjust=0., face="bold", size=12),
plot.subtitle = element_text(hjust=0, size=8),
plot.caption = element_text(hjust=0.95),
# Remove grid and axis ticks/ text
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
# Remove legend
legend.position = "none"
)
#### Plot 2 graphs together
(p6 / p7)
```
The insights garnered from the heat map analysis offer a nuanced understanding of the tactical preferences and playing styles of Borussia Dortmund and Bayern Munich. Our findings indicate that Borussia Dortmund exhibits a versatile and balanced approach in their gameplay, utilizing both the left and right flanks effectively. This tactical diversity suggests a dynamic and adaptable strategy, providing various avenues for offensive play.
In contrast, Bayern Munich displays a more concentrated and focused strategy in their gameplay. While this approach has proven successful for them, it denotes a less varied tactical execution compared to Borussia Dortmund.
Based on these strategic observations, coupled with our comprehensive analysis of both teams' performance metrics and developmental environments, we are confident in recommending Borussia Dortmund as the ideal destination for our striker.
### d) What player should they replace?
As we progress in our analysis, our confidence in Borussia Dortmund's capability to foster the development of our emerging striker remains steadfast. The next crucial step in our evaluation process involves identifying a suitable candidate within Dortmund's current squad whom our striker could potentially replace. To this end, we have narrowed our focus to three prominent strikers from Dortmund: Erling Haaland, Marco Reus, and Jadon Sancho.
Our methodology for this phase of analysis will entail a detailed examination of various performance metrics for each of these players. We plan to deploy a scatter plot analysis to assess their goal-scoring positions, which will provide insights into their spatial effectiveness and preferred areas on the pitch. Additionally, we will scrutinize the accuracy of their shots, a critical metric in determining a striker's efficiency in front of goal. Moreover, the effectiveness of key passes will be evaluated, as this aspect is pivotal in understanding a player's ability to create goal-scoring opportunities for the team.
By juxtaposing these analytical insights against the skill set and potential of our striker, we aim to identify the most strategic fit within the Dortmund squad.
```{r, warning =FALSE, message = FALSE}
goals_dortmund_Erling <- left_join(new_Data, players, by = c("shooterID" = "playerID"))%>%
filter(homeTeamID == 129 | awayTeamID == 129) %>%
filter(shooterID == 8260)%>%
filter(shotResult == "Goal")
goals_dortmund_Marco <- left_join(new_Data, players, by = c("shooterID" = "playerID"))%>%
filter(homeTeamID == 129 | awayTeamID == 129) %>%
filter(shooterID == 319)%>%
filter(shotResult == "Goal")
goals_dortmund_Sancho <- left_join(new_Data, players, by = c("shooterID" = "playerID"))%>%
filter(homeTeamID == 129 | awayTeamID == 129) %>%
filter(shooterID == 6345) %>%
filter(shotResult == "Goal")
custom_colors <- c("OpenPlay" = "#e66101",
"FromCorner" = "#fdb863",
"SetPiece" = "#999999",
"Penalty" = "#b2abd2",
"DirectFreekick" = "#5e3c99")
sancho_goals <- ggplot(goals_dortmund_Sancho) +
annotate_pitch(colour = "white",
fill = "#05205e",
limits = FALSE) +
geom_point(aes(x = positionX*100, y = positionY*100, color = situation),
# colour = "yellow",
size = 4) +
scale_color_manual(values = custom_colors) +
theme_pitch() +
theme(panel.background = element_rect(fill = "#05205e")) +
ggtitle("Simple shotmap",
"ggsoccer example")+
coord_flip(xlim = c(50, 101)) +
scale_y_reverse() +
labs(color = "Game Play",
title = "Sancho scores within the box with different game plays",
subtitle = "Scatter plot showing goal scoring positions by different game plays"
)+
theme(
plot.title = element_text(hjust = 0, face="bold", size=18), # Left align title
plot.subtitle = element_text(hjust = 0) # Left align subtitle
)
erling_goals <- ggplot(goals_dortmund_Erling) +
annotate_pitch(colour = "white",
fill = "#05205e",
limits = FALSE) +
geom_point(aes(x = positionX*100, y = positionY*100, color = situation),
# colour = "yellow",
size = 4) +
scale_color_manual(values = custom_colors) +
theme_pitch() +
theme(panel.background = element_rect(fill = "#05205e")) +
ggtitle("Simple shotmap",
"ggsoccer example")+
coord_flip(xlim = c(50, 101)) +
scale_y_reverse() +
labs(color = "Game Play",
title = "Haaland scores within the box with Open play and Penalty",
subtitle = "Scatter plot showing goal scoring positions by different game plays"
)+
theme(
plot.title = element_text(hjust = 0, face="bold", size=18), # Left align title
plot.subtitle = element_text(hjust = 0) # Left align subtitle
)
marco_goals <- ggplot(goals_dortmund_Marco) +
annotate_pitch(colour = "white",
fill = "#05205e",
limits = FALSE) +
geom_point(aes(x = positionX*100, y = positionY*100, color = situation),
# colour = "yellow",
size = 4) +
scale_color_manual(values = custom_colors) +
theme_pitch() +
theme(panel.background = element_rect(fill = "#05205e")) +
ggtitle("Simple shotmap",
"ggsoccer example")+
coord_flip(xlim = c(50, 101)) +
scale_y_reverse() +
labs(color = "Game Play",
title = "Marco scores from wihtin and outside the box with different game plays",
subtitle = "Scatter plot showing goal scoring positions by different game plays"
)+
theme(
plot.title = element_text(hjust = 0, face="bold", size=18), # Left align title
plot.subtitle = element_text(hjust = 0) # Left align subtitle
)