library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ stringr 1.5.0
## ✔ purrr   1.0.2     ✔ tibble  3.2.1
## ✔ readr   2.1.4     ✔ tidyr   1.3.0
## ── 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
library(pastecs)
## Warning: package 'pastecs' was built under R version 4.3.2
## 
## Attaching package: 'pastecs'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
library(DataExplorer)
## Warning: package 'DataExplorer' was built under R version 4.3.2
library(tidyr)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(cluster)
#Importing Data
game_stats <- read_excel("General_stats.xlsx")
shoot_stats <- read_excel("Shoot_stats.xlsx")
#Display summary statistics
summary_game_stats <- summary(game_stats)
summary_shoot_stats <- summary(shoot_stats)
print(summary_game_stats)
##     Player             Nation              Pos                 Age       
##  Length:723         Length:723         Length:723         Min.   :15.00  
##  Class :character   Class :character   Class :character   1st Qu.:20.50  
##  Mode  :character   Mode  :character   Mode  :character   Median :24.00  
##                                                           Mean   :24.51  
##                                                           3rd Qu.:28.00  
##                                                           Max.   :40.00  
##                                                                          
##        MP            Starts           Min           Min90s     
##  Min.   : 0.00   Min.   : 0.00   Min.   :   1   Min.   : 0.00  
##  1st Qu.: 1.00   1st Qu.: 0.00   1st Qu.: 370   1st Qu.: 4.10  
##  Median :15.00   Median : 7.00   Median :1131   Median :12.60  
##  Mean   :15.69   Mean   :11.56   Mean   :1321   Mean   :14.67  
##  3rd Qu.:27.00   3rd Qu.:21.00   3rd Qu.:2126   3rd Qu.:23.60  
##  Max.   :38.00   Max.   :38.00   Max.   :3420   Max.   :38.00  
##                                  NA's   :154    NA's   :154    
##       Gls              Ast              G_A              G-PK       
##  Min.   : 0.000   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000  
##  Median : 0.000   Median : 0.000   Median : 1.000   Median : 0.000  
##  Mean   : 1.826   Mean   : 1.276   Mean   : 3.102   Mean   : 1.696  
##  3rd Qu.: 2.000   3rd Qu.: 2.000   3rd Qu.: 4.000   3rd Qu.: 2.000  
##  Max.   :36.000   Max.   :16.000   Max.   :44.000   Max.   :29.000  
##  NA's   :154      NA's   :154      NA's   :154      NA's   :154     
##        PK           PKatt            CrdY             CrdR        
##  Min.   :0.00   Min.   :0.000   Min.   : 0.000   Min.   :0.00000  
##  1st Qu.:0.00   1st Qu.:0.000   1st Qu.: 0.000   1st Qu.:0.00000  
##  Median :0.00   Median :0.000   Median : 2.000   Median :0.00000  
##  Mean   :0.13   Mean   :0.174   Mean   : 2.448   Mean   :0.05272  
##  3rd Qu.:0.00   3rd Qu.:0.000   3rd Qu.: 4.000   3rd Qu.:0.00000  
##  Max.   :7.00   Max.   :8.000   Max.   :14.000   Max.   :2.00000  
##  NA's   :154    NA's   :154     NA's   :154      NA's   :154      
##        xG             npxG             xAG            npxG_xAG     
##  Min.   : 0.00   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 0.10   1st Qu.: 0.100   1st Qu.: 0.100   1st Qu.: 0.300  
##  Median : 0.80   Median : 0.800   Median : 0.600   Median : 1.500  
##  Mean   : 1.94   Mean   : 1.803   Mean   : 1.395   Mean   : 3.199  
##  3rd Qu.: 2.20   3rd Qu.: 2.200   3rd Qu.: 1.900   3rd Qu.: 4.300  
##  Max.   :28.40   Max.   :23.000   Max.   :16.700   Max.   :28.300  
##  NA's   :154     NA's   :154      NA's   :154      NA's   :154     
##       PrgC             PrgP             PrgR            Gls90       
##  Min.   :  0.00   Min.   :  0.00   Min.   :  0.00   Min.   :0.0000  
##  1st Qu.:  2.00   1st Qu.:  6.00   1st Qu.:  2.00   1st Qu.:0.0000  
##  Median : 13.00   Median : 32.00   Median : 21.00   Median :0.0000  
##  Mean   : 22.67   Mean   : 50.43   Mean   : 49.95   Mean   :0.1036  
##  3rd Qu.: 32.00   3rd Qu.: 79.00   3rd Qu.: 71.00   3rd Qu.:0.1500  
##  Max.   :179.00   Max.   :266.00   Max.   :520.00   Max.   :1.2700  
##  NA's   :154      NA's   :154      NA's   :154      NA's   :154     
##      Ast90             G_A90           G-PK90           G_A-PK90     
##  Min.   :0.00000   Min.   :0.000   Min.   :0.00000   Min.   :0.0000  
##  1st Qu.:0.00000   1st Qu.:0.000   1st Qu.:0.00000   1st Qu.:0.0000  
##  Median :0.00000   Median :0.090   Median :0.00000   Median :0.0900  
##  Mean   :0.07538   Mean   :0.179   Mean   :0.09817   Mean   :0.1735  
##  3rd Qu.:0.11000   3rd Qu.:0.280   3rd Qu.:0.14000   3rd Qu.:0.2800  
##  Max.   :1.05000   Max.   :2.120   Max.   :1.27000   Max.   :2.1200  
##  NA's   :154       NA's   :154     NA's   :154       NA's   :154     
##       xG90            xAG90            xG_xAG90          npxG90      
##  Min.   :0.0000   Min.   :0.00000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0200   1st Qu.:0.01000   1st Qu.:0.0500   1st Qu.:0.0200  
##  Median :0.0700   Median :0.06000   Median :0.1500   Median :0.0700  
##  Mean   :0.1309   Mean   :0.08738   Mean   :0.2182   Mean   :0.1249  
##  3rd Qu.:0.1800   3rd Qu.:0.13000   3rd Qu.:0.3300   3rd Qu.:0.1800  
##  Max.   :0.9200   Max.   :1.95000   Max.   :2.2700   Max.   :0.9000  
##  NA's   :154      NA's   :154       NA's   :154      NA's   :154     
##    npxG_xAG90         Club          
##  Min.   :0.0000   Length:723        
##  1st Qu.:0.0500   Class :character  
##  Median :0.1400   Mode  :character  
##  Mean   :0.2122                     
##  3rd Qu.:0.3200                     
##  Max.   :2.2700                     
##  NA's   :154
print(summary_shoot_stats)
##     Player             Nation              Pos                 Age       
##  Length:569         Length:569         Length:569         Min.   :15.00  
##  Class :character   Class :character   Class :character   1st Qu.:22.00  
##  Mode  :character   Mode  :character   Mode  :character   Median :25.00  
##                                                           Mean   :25.32  
##                                                           3rd Qu.:29.00  
##                                                           Max.   :37.00  
##                                                                          
##       90s             Gls               Sh              SoT        
##  Min.   : 0.00   Min.   : 0.000   Min.   :  0.00   Min.   : 0.000  
##  1st Qu.: 4.10   1st Qu.: 0.000   1st Qu.:  2.00   1st Qu.: 0.000  
##  Median :12.60   Median : 0.000   Median :  9.00   Median : 2.000  
##  Mean   :14.67   Mean   : 1.826   Mean   : 16.71   Mean   : 5.548  
##  3rd Qu.:23.60   3rd Qu.: 2.000   3rd Qu.: 25.00   3rd Qu.: 7.000  
##  Max.   :38.00   Max.   :36.000   Max.   :124.00   Max.   :54.000  
##                                                                    
##  SoT_percentage       Sh_90            SoT_90            G_Sh        
##  Min.   :  0.00   Min.   : 0.000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.: 19.10   1st Qu.: 0.280   1st Qu.:0.0000   1st Qu.:0.00000  
##  Median : 28.80   Median : 0.860   Median :0.2000   Median :0.05000  
##  Mean   : 29.62   Mean   : 1.222   Mean   :0.3713   Mean   :0.08009  
##  3rd Qu.: 40.00   3rd Qu.: 1.810   3rd Qu.:0.5600   3rd Qu.:0.13000  
##  Max.   :100.00   Max.   :15.000   Max.   :7.5000   Max.   :1.00000  
##  NA's   :102                                        NA's   :102      
##      G_SoT             Dist             FK               PK        
##  Min.   :0.0000   Min.   : 3.80   Min.   : 0.000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:12.90   1st Qu.: 0.000   1st Qu.:0.0000  
##  Median :0.2500   Median :16.30   Median : 0.000   Median :0.0000  
##  Mean   :0.2683   Mean   :16.73   Mean   : 0.529   Mean   :0.1301  
##  3rd Qu.:0.4300   3rd Qu.:19.95   3rd Qu.: 0.000   3rd Qu.:0.0000  
##  Max.   :1.0000   Max.   :34.10   Max.   :17.000   Max.   :7.0000  
##  NA's   :173      NA's   :102                                      
##      PKatt             xG             npxG           npxG_Sh      
##  Min.   :0.000   Min.   : 0.00   Min.   : 0.000   Min.   :0.0100  
##  1st Qu.:0.000   1st Qu.: 0.10   1st Qu.: 0.100   1st Qu.:0.0600  
##  Median :0.000   Median : 0.80   Median : 0.800   Median :0.0900  
##  Mean   :0.174   Mean   : 1.94   Mean   : 1.803   Mean   :0.0985  
##  3rd Qu.:0.000   3rd Qu.: 2.20   3rd Qu.: 2.200   3rd Qu.:0.1200  
##  Max.   :8.000   Max.   :28.40   Max.   :23.000   Max.   :0.3400  
##                                                   NA's   :102     
##       G_xG            np_G_xG             SCA             SCA90       
##  Min.   :-4.6000   Min.   :-4.8000   Min.   :  0.00   Min.   : 0.000  
##  1st Qu.:-0.6000   1st Qu.:-0.6000   1st Qu.:  4.00   1st Qu.: 0.730  
##  Median :-0.1000   Median :-0.1000   Median : 18.00   Median : 1.790  
##  Mean   :-0.1142   Mean   :-0.1072   Mean   : 29.46   Mean   : 2.151  
##  3rd Qu.: 0.2000   3rd Qu.: 0.2000   3rd Qu.: 41.00   3rd Qu.: 2.990  
##  Max.   : 8.5000   Max.   : 8.3000   Max.   :219.00   Max.   :90.000  
##                                                                       
##  SCA_Pass_Live    SCA_Pass_Dead        SCA_TO           SCA_Sh      
##  Min.   :  0.00   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:  3.00   1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000  
##  Median : 14.00   Median : 0.000   Median : 0.000   Median : 1.000  
##  Mean   : 21.41   Mean   : 2.573   Mean   : 1.622   Mean   : 1.947  
##  3rd Qu.: 31.00   3rd Qu.: 1.000   3rd Qu.: 2.000   3rd Qu.: 3.000  
##  Max.   :164.00   Max.   :94.000   Max.   :19.000   Max.   :14.000  
##                                                                     
##     SCA_Fld          SCA_Def            GCA             GCA90        
##  Min.   : 0.000   Min.   :0.0000   Min.   : 0.000   Min.   : 0.0000  
##  1st Qu.: 0.000   1st Qu.:0.0000   1st Qu.: 0.000   1st Qu.: 0.0000  
##  Median : 0.000   Median :0.0000   Median : 2.000   Median : 0.1100  
##  Mean   : 1.422   Mean   :0.4798   Mean   : 3.153   Mean   : 0.2116  
##  3rd Qu.: 2.000   3rd Qu.:1.0000   3rd Qu.: 4.000   3rd Qu.: 0.3000  
##  Max.   :17.000   Max.   :5.0000   Max.   :31.000   Max.   :11.2500  
##                                                                      
##  GCA_Pass_Live    GCA_Pass_Dead        GCA_TO           GCA_Sh      
##  Min.   : 0.000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.: 0.000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median : 1.000   Median :0.0000   Median :0.0000   Median :0.0000  
##  Mean   : 2.214   Mean   :0.1845   Mean   :0.2197   Mean   :0.2812  
##  3rd Qu.: 3.000   3rd Qu.:0.0000   3rd Qu.:0.0000   3rd Qu.:0.0000  
##  Max.   :24.000   Max.   :5.0000   Max.   :5.0000   Max.   :5.0000  
##                                                                     
##     GCA_Fld          GCA_Def            Club          
##  Min.   :0.0000   Min.   :0.00000   Length:569        
##  1st Qu.:0.0000   1st Qu.:0.00000   Class :character  
##  Median :0.0000   Median :0.00000   Mode  :character  
##  Mean   :0.1916   Mean   :0.06151                     
##  3rd Qu.:0.0000   3rd Qu.:0.00000                     
##  Max.   :4.0000   Max.   :2.00000                     
## 
# Fill missing values with 0
game_stats <- game_stats %>% mutate_all(~ ifelse(is.na(.), 0, .))
shoot_stats <- shoot_stats %>% mutate_all(~ ifelse(is.na(.), 0, .))
#Remove Duplicate BY summarise the stat of each player
game_stats <- game_stats %>% mutate(modified_pos = 
case_when(Pos %in% c("FW")~"FW",Pos %in% c("MF","MF,DF", "FW,MF","MF,FW")~"MF", Pos %in% c("DF","DF,MF") ~ "DF",TRUE ~ Pos))
shoot_stats <- shoot_stats %>% mutate(modified_pos = 
case_when(Pos %in% c("FW","FW,MF","MF,FW")~"FW",Pos %in% c("MF","MF,DF","FW,MF","MF,FW")~"MF", Pos %in% c("DF","DF,MF")~"DF",TRUE ~ Pos))
game_stats_new <- game_stats %>% group_by(Player, modified_pos) %>%
                  summarise(Min = sum(Min), Gls = sum(Gls), Ast = sum(Ast), PrgC = sum(PrgC), PrgP = sum(PrgP), 
                            PrgR = sum(PrgR),.groups = "drop")
shoot_stats_new <- shoot_stats %>% group_by(Player, modified_pos) %>%
                    summarise(Shoot = sum(Sh), SoT_percentage = sum(SoT_percentage), SCA = sum(SCA), GCA = sum(GCA),
                              .groups = "drop")
# Select Unique Collomn from each data 
game_stats_new <- game_stats_new[,c("Player","modified_pos","Min","Gls","Ast", "PrgC","PrgP","PrgR")]
shoot_stats_new <- shoot_stats_new[, c("Player","Shoot","SoT_percentage","SCA","GCA")]
# Merge data by player name
Merge_data <- merge(game_stats_new, shoot_stats_new, by = "Player", all.x = TRUE)
#Filter selected feature and remove unfit data 
Merge_data <- Merge_data %>% filter(Min > 90) %>% filter(modified_pos %in% c("FW")) 
#Select numerical metric columns for clustering
rownames(Merge_data) <- Merge_data$Player 
player_data <- Merge_data[, c("Min","Gls","Ast","PrgC","PrgP","PrgR","Shoot","SoT_percentage","SCA","GCA")]
# Normalize numeric data by scaling
scaled_data <- scale(player_data)
# Calculate within-cluster sum of squares (WCSS) for different values of k
wss <- function(k) {
  kmeans_result <- kmeans(scaled_data, centers = k)
  return(sum(kmeans_result$withinss))}
k_values <- 1:10
wss_values <- sapply(k_values, wss)

#Plot the elbow curve
plot(k_values, wss_values, type = "b", pch = 19, frame = FALSE, 
     xlab = "Number of Clusters (k)", ylab = "Total Within-Cluster Sum of Squares",
     main = "Elbow Method")
optimal_k <- which(diff(wss_values) == max(diff(wss_values)))
points(optimal_k, wss_values[optimal_k], col = "red", cex = 2, pch = 19)

#Create kmeans model
kmeans_result <- kmeans(scaled_data, centers = optimal_k)
player_data$Cluster <- as.factor(kmeans_result$cluster)
print(kmeans_result)
## K-means clustering with 8 clusters of sizes 11, 14, 2, 17, 7, 8, 9, 1
## 
## Cluster means:
##           Min        Gls        Ast         PrgC        PrgP        PrgR
## 1 -1.05553342 -0.7617504 -0.7382160 -0.699615905 -0.75669629 -0.80862224
## 2  0.09363356 -0.1991590  0.1197761  0.017875897 -0.06048255 -0.04382690
## 3  2.00859235  3.8751968  1.0635675  0.321277581  2.14532085  0.63104762
## 4 -0.54374417 -0.4774636 -0.6694675 -0.497708664 -0.56771977 -0.54414923
## 5 -0.65425730 -0.3120505 -0.5944444 -0.573063331 -0.63814757 -0.64068975
## 6  0.93688323  0.8758757  0.4386245  0.203288038  0.29364708  0.47263242
## 7  1.45872756  0.8259868  1.5198750  1.960792419  1.79537489  2.03231380
## 8 -0.51734900 -0.7225649  2.6705635  0.002011757  0.49049168 -0.09019232
##        Shoot SoT_percentage         SCA         GCA
## 1 -0.8749021    -1.30901204 -0.88572644 -0.82483996
## 2 -0.1890319    -0.21951735 -0.05739815  0.02851983
## 3  2.6695887     0.27833179  1.57029961  1.75888673
## 4 -0.5260391     0.09512683 -0.61394144 -0.70460755
## 5 -0.6809142     1.98403879 -0.69254990 -0.55957872
## 6  1.2557240    -0.03229704  0.57435447  0.43283759
## 7  1.1208062    -0.06043667  1.88604103  1.75009038
## 8  0.5072074     2.21259041  1.12161443  1.83805384
## 
## Clustering vector:
##       Aleksandar Mitrović            Alexander Isak       Allan Saint-Maximin 
##                         6                         2                         2 
##            Anthony Elanga            Anthony Gordon           Anthony Martial 
##                         4                         5                         4 
##                    Antony             Armando Broja              Bryan Mbeumo 
##                         6                         5                         7 
##               Bukayo Saka             Callum Wilson           Carlos Vinícius 
##                         7                         6                         5 
##                 Che Adams                Chris Wood         Cristiano Ronaldo 
##                         2                         5                         1 
##     Crysencio Summerville              Daniel James                Danny Ings 
##                         2                         4                         5 
##             Danny Welbeck              Darwin Núñez               Diego Costa 
##                         6                         6                         4 
##                Diogo Jota     Dominic Calvert-Lewin             Eddie Nketiah 
##                         2                         4                         4 
##               Ellis Simms            Erling Haaland             Evan Ferguson 
##                         1                         3                         4 
##             Gabriel Jesus        Gabriel Martinelli         Gianluca Scamacca 
##                         6                         7                         4 
##                Harry Kane              Harry Wilson                Ivan Toney 
##                         3                         2                         6 
##             Jack Grealish              Jacob Murphy               Jamie Vardy 
##                         7                         2                         2 
##      Jean-Philippe Mateta              Joe Gelhardt              Kevin Schade 
##                         1                         1                         4 
##             Kieffer Moore          Leandro Trossard                 Luis Díaz 
##                         4                         8                         2 
##           Luis Sinisterra             Manor Solomon           Marcus Rashford 
##                         4                         5                         7 
##             Matheus Cunha             Maxwel Cornet           Michail Antonio 
##                         1                         1                         2 
##            Miguel Almirón             Mohamed Salah               Neal Maupay 
##                         7                         7                         4 
##           Neeskens Kebano              Noni Madueke           Odsonne Édouard 
##                         1                         4                         2 
##             Ollie Watkins           Patrick Bamford               Patson Daka 
##                         6                         2                         2 
##              Paul Onuachu                Phil Foden Pierre-Emerick Aubameyang 
##                         4                         7                         4 
##              Raúl Jiménez              Reiss Nelson               Ryan Fraser 
##                         4                         5                         1 
##            Salomón Rondón              Sam Surridge             Taiwo Awoniyi 
##                         1                         1                         4 
##                      Tetê                   Willian               Yoane Wissa 
##                         1                         7                         2 
## 
## Within cluster sum of squares by cluster:
## [1]  8.094487 20.693847  9.224843 15.748262 10.489220 22.338415 56.739518
## [8]  0.000000
##  (between_SS / total_SS =  78.9 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Within cluster sum of squares by cluster: [1] 5.884772 25.613953 12.471701 7.776107 36.602154 7.104416 35.895025 3.829411 (between_SS / total_SS = 80.1 %) indicates good fit within the cluster group

player_data$Cluster <- as.numeric(kmeans_result$cluster)
fviz_cluster(object = kmeans_result,
             data = player_data)

kmeans_result$centers
##           Min        Gls        Ast         PrgC        PrgP        PrgR
## 1 -1.05553342 -0.7617504 -0.7382160 -0.699615905 -0.75669629 -0.80862224
## 2  0.09363356 -0.1991590  0.1197761  0.017875897 -0.06048255 -0.04382690
## 3  2.00859235  3.8751968  1.0635675  0.321277581  2.14532085  0.63104762
## 4 -0.54374417 -0.4774636 -0.6694675 -0.497708664 -0.56771977 -0.54414923
## 5 -0.65425730 -0.3120505 -0.5944444 -0.573063331 -0.63814757 -0.64068975
## 6  0.93688323  0.8758757  0.4386245  0.203288038  0.29364708  0.47263242
## 7  1.45872756  0.8259868  1.5198750  1.960792419  1.79537489  2.03231380
## 8 -0.51734900 -0.7225649  2.6705635  0.002011757  0.49049168 -0.09019232
##        Shoot SoT_percentage         SCA         GCA
## 1 -0.8749021    -1.30901204 -0.88572644 -0.82483996
## 2 -0.1890319    -0.21951735 -0.05739815  0.02851983
## 3  2.6695887     0.27833179  1.57029961  1.75888673
## 4 -0.5260391     0.09512683 -0.61394144 -0.70460755
## 5 -0.6809142     1.98403879 -0.69254990 -0.55957872
## 6  1.2557240    -0.03229704  0.57435447  0.43283759
## 7  1.1208062    -0.06043667  1.88604103  1.75009038
## 8  0.5072074     2.21259041  1.12161443  1.83805384
team_stats <- read_excel("team_stats.xlsx")
team_stats
## # A tibble: 20 × 32
##    Club    N_Pl   Age  Poss Playing_Time_MP Playing_Time_Starts Playing_Time_Min
##    <chr>  <dbl> <dbl> <dbl>           <dbl>               <dbl>            <dbl>
##  1 Arsen…    26   247   593              38                 418             3.42
##  2 Aston…    26   270   493              38                 418             3.42
##  3 Bourn…    31   263   404              38                 418             3.42
##  4 Brent…    25   262   438              38                 418             3.42
##  5 Brigh…    29   263   602              38                 418             3.42
##  6 Chels…    32   263   587              38                 418             3.42
##  7 Cryst…    26   267   463              38                 418             3.42
##  8 Evert…    28   266   428              38                 418             3.42
##  9 Fulham    29   282   488              38                 418             3.42
## 10 Leeds…    29   253   470              38                 418             3.42
## 11 Leice…    28   261   477              38                 418             3.42
## 12 Liver…    28   274   608              38                 418             3.42
## 13 Manch…    24   272   647              38                 418             3.42
## 14 Manch…    26   269   537              38                 418             3.42
## 15 Newca…    27   273   523              38                 418             3.42
## 16 Nott'…    33   265   376              38                 418             3.42
## 17 South…    36   247   445              38                 418             3.42
## 18 Totte…    29   276   500              38                 418             3.42
## 19 West …    25   282   421              38                 418             3.42
## 20 Wolves    32   267   499              38                 418             3.42
## # ℹ 25 more variables: Playing_Time_90s <dbl>, Performance_Gls <dbl>,
## #   Performance_Ast <dbl>, `Performance_G+A` <dbl>, `Performance_G-PK` <dbl>,
## #   Performance_PK <dbl>, Performance_PKatt <dbl>, Performance_CrdY <dbl>,
## #   Performance_CrdR <dbl>, Expected_xG <dbl>, Expected_npxG <dbl>,
## #   Expected_xAG <dbl>, `Expected_npxG+xAG` <dbl>, Progression_PrgC <dbl>,
## #   Progression_PrgP <dbl>, Per_90_Minutes_Gls <dbl>, Per_90_Minutes_Ast <dbl>,
## #   `Per_90_Minutes_G+A` <dbl>, `Per_90_Minutes_G-PK` <dbl>, …
ggplot(team_stats, aes(x = Club)) +
  geom_bar(aes(y = Performance_Gls), stat = "identity", position = "stack", fill = "blue", alpha = 0.7) +
  geom_bar(aes(y = Expected_xG), stat = "identity", position = "dodge", fill = "red", alpha = 0.7) +
  labs(title = "Comparison between Goals and Expected Goals by Premier Club 2022-2023 Season",
       x = "Club",
       y = "Values") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(y = Performance_Gls, label = Performance_Gls, fill = "Performance_Gls"), position = position_dodge(width = 0.9), vjust = 1.5, size = 3, color = "black") +
  geom_text(aes(y = Expected_xG, label = Expected_xG, fill = "Expected_xG"), position = position_dodge(width = 0.9), vjust = -0.5, size = 3, color = "black")
## Warning in geom_text(aes(y = Performance_Gls, label = Performance_Gls, fill =
## "Performance_Gls"), : Ignoring unknown aesthetics: fill
## Warning in geom_text(aes(y = Expected_xG, label = Expected_xG, fill =
## "Expected_xG"), : Ignoring unknown aesthetics: fill

ggplot(team_stats, aes(x = Club)) +
  geom_bar(aes(y = Performance_Gls, fill = "Performance_Gls"), stat = "identity", position = "stack", alpha = 0.7) +
  geom_bar(aes(y = Expected_xG, fill = "Expected_xG"), stat = "identity", position = "stack", alpha = 0.7) +
  labs(title = "Comparison between Goals and Expected Goals by Premier Club 2022-2023 Season",
       x = "Club",
       y = "Values") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_fill_manual(values = c("blue", "red"), name = "Variable") +
  geom_text(aes(y = Performance_Gls, label = Performance_Gls, fill = "Performance_Gls"), position = position_dodge(width = 0.9), vjust = 1.5, size = 3, color = "black") +
  geom_text(aes(y = Expected_xG, label = Expected_xG, fill = "Expected_xG"), 
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3, color = "black") +
  guides(fill = guide_legend(title = "Variable"))
## Warning in geom_text(aes(y = Performance_Gls, label = Performance_Gls, fill =
## "Performance_Gls"), : Ignoring unknown aesthetics: fill
## Warning in geom_text(aes(y = Expected_xG, label = Expected_xG, fill =
## "Expected_xG"), : Ignoring unknown aesthetics: fill