library(jsonlite)
library(httr)
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(tidyr)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:httr':
## 
##     config
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
Teams_2023_stats <- read.csv("/Users/anuragreddy/MCP/all_2023_teams_stats.csv")
Teams_2024_stats <- read.csv("/Users/anuragreddy/MCP/all_2024_teams_stats.csv")

Let’s find number of major penalties, Game Misconducts, 10-min-misconduct, match penalty were called in 2022-23 season

penalties_23 <- Teams_2023_stats |>
  select(conference_name,Penalties,Minor.Penalty,Major.Penalty,Disqualifications,Game.Misconduct,X10.minute.misconduct,Penalty.match,Penalty.Minutes)|>
  group_by(conference_name)
  
penalties_totals_23 <- penalties_23|>
  group_by(conference_name)|>
  summarize(
    Total_Penalites = sum(Penalties, na.rm = TRUE),
    Total_Minor_Penalty = sum(Minor.Penalty, na.rm = TRUE),
    Total_Major_Penalty = sum(Major.Penalty, na.rm = TRUE),
    Total_Disqualifications = sum(Disqualifications, na.rm = TRUE),
    Total_Game_Misconduct = sum(Game.Misconduct, na.rm = TRUE),
    Total_10_Minute_Misconduct = sum(X10.minute.misconduct, na.rm = TRUE),
    Total_Penalty_Match = sum(Penalty.match, na.rm = TRUE),
    Total_Penalty_Minutes = sum(Penalty.Minutes, na.rm = TRUE),
  )
#View(penalties_totals_23)
Major_Penalties_23 <- penalties_totals_23 |>
  ggplot(aes(x=conference_name))+
  geom_bar(aes(y=Total_Major_Penalty, fill="Major Penalty"), stat = "identity")+
  geom_bar(aes(y=Total_Game_Misconduct, fill="Game Misconduct"), stat = "identity")+
  geom_bar(aes(y=Total_Disqualifications, fill="Disqualifications"), stat = "identity")+
  #geom_bar(aes(y=Total_Penalty_Match, fill="Penalty Match"), stat = "identity")+
  ggtitle("Penalities in each conference in 2022-23 season")+
  scale_fill_manual(values = c("Major Penalty" = "lightgreen", "Game Misconduct" = "orange","Disqualifications" = "lightblue","Penalty Match"="maroon")) +
  labs(fill = "Penalty Type",y="Count of Penalities") +
  theme_classic()

ggplotly(Major_Penalties_23)
penalties_24 <- Teams_2024_stats |>
  select(conference_name,Penalties,Minor.Penalty,Major.Penalty,Disqualifications,Game.Misconduct,X10.minute.misconduct,Penalty.Minutes)
  

penalties_totals_24 <- penalties_24|>
  group_by(conference_name)|>
  summarize(
    Total_Penalites = sum(Penalties, na.rm = TRUE),
    Total_Minor_Penalty = sum(Minor.Penalty, na.rm = TRUE),
    Total_Major_Penalty = sum(Major.Penalty, na.rm = TRUE),
    Total_Disqualifications = sum(Disqualifications, na.rm = TRUE),
    Total_Game_Misconduct = sum(Game.Misconduct, na.rm = TRUE),
    Total_10_Minute_Misconduct = sum(X10.minute.misconduct, na.rm = TRUE),
    Total_Penalty_Minutes = sum(Penalty.Minutes, na.rm = TRUE),
  )

#View(penalties_totals_24)
Major_Penalties_24 <- penalties_totals_24 |>
  ggplot(aes(x=conference_name))+
  geom_bar(aes(y=Total_Major_Penalty, fill="Major Penalty"), stat = "identity")+
  geom_bar(aes(y=Total_Game_Misconduct, fill="Game Misconduct"), stat = "identity")+
  geom_bar(aes(y=Total_Disqualifications, fill="Disqualifications"), stat = "identity")+
  ggtitle("Penalities in each conference in 2023-24 season")+
  scale_fill_manual(values = c("Major Penalty" = "lightgreen", "Game Misconduct" = "orange","Disqualifications" = "lightblue","Penalty Match"="maroon")) +
  labs(fill = "Penalty Type",y="Count of Penalities") +
  theme_classic()

ggplotly(Major_Penalties_24)