-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLB EV vs SLG 2023
105 lines (86 loc) · 3.77 KB
/
MLB EV vs SLG 2023
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
install.packages("baseballr")
install.packages("mlbplotR")
library(tidyverse)
library(dplyr)
library(readr)
library(ggplot2)
library(ggrepel)
library(tidyr)
library(ggimage)
library(hockeyR)
library(mlbplotR)
library(ggimage)
help(package = "baseballr")
tictoc::tic()
progressr::with_progress({
mlb_stat_names <- baseballr::mlb_baseball_stats() #select season
})
tictoc::toc()
tictoc::tic()
progressr::with_progress({
mlb_logos <- baseballr::mlb_teams() #select season
})
tictoc::toc()
mlb_logos <- mlb_logos %>%
filter(league_id %in% c("103", "104"))
mlb_logos <- mlbplotR::load_mlb_teams() |>
dplyr::filter(!team_abbr %in% c("AL", "NL", "MLB")) |>
# Get team batting leaderboard data
tictoc::tic()
progressr::with_progress({
team_batting_data <- baseballr::fg_team_batter
})
tictoc::toc()
head(team_batting_data())
# Define the date range for the season
start_date <- "2023-03-30"
end_date <- "2023-10-01"
standard_batting <- read.csv("C:\\Users\\imksy\\Documents\\baseball_reference_slg.csv")
advanced_batting <- read.csv("C:\\Users\\imksy\\Documents\\baseball_reference_ev.csv")
# Merge the datasets based on a common identifier (e.g., team)
combined_batting <- merge(standard_batting, advanced_batting, by = c("Tm"))
# Merge the datasets based on a common identifier (e.g., team)
combined_batting_logos <- merge(combined_batting, mlb_logos, by.x = "Tm", by.y = "team_name")
avg_ev <- mean(combined_batting_logos$EV, na.rm = TRUE)
avg_slg <- mean(combined_batting_logos$SLG, na.rm = TRUE)
# Calculate non_HR_slugging
combined_batting_logos$non_HR_slugging <- ((combined_batting_logos$H - (combined_batting_logos$X2B + combined_batting_logos$X3B) + (2 * combined_batting_logos$X2B) + (3 * combined_batting_logos$X3B)) / (combined_batting_logos$AB - combined_batting_logos$HR))
#might need to also adjust for not accounting for ev hits without HR
avg_nonHr_slg <- mean(combined_batting_logos$non_HR_slugging, na.rm = TRUE)
correlation_coefficient_ev_slg <- cor(combined_batting_logos$EV, combined_batting_logos$SLG, use = "complete.obs")
correlation_coefficient_ev_slg_noHR <- cor(combined_batting_logos$EV, combined_batting_logos$non_HR_slugging, use = "complete.obs")
ggplot(combined_batting_logos, aes(x = EV, y = SLG)) +
geom_image(aes(image = team_logo_espn), size = 0.1) +
geom_vline(xintercept = avg_ev, linetype = "dashed") +
geom_hline(yintercept = avg_slg, linetype = "dashed") +
labs(title = "Slugging Percentage vs Exit Velocity by Team, 2023 MLB Season",
x = "Exit Velocity (EV)",
y = "Slugging Percentage (SLG)",
color = "Team") +
scale_x_continuous(
limits = c(87, 91) #breaks
) +
scale_y_continuous(limits = c(0.37, 0.501)) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_line(color = "gray", linetype = "dashed")
) +
annotate("text", x = 91, y = 0.4, label = paste("Correlation:", round(correlation_coefficient_ev_slg, 5)),
hjust = 1, vjust = 0, size = 3, color = "red")
ggplot(combined_batting_logos, aes(x = EV, y = non_HR_slugging)) +
geom_image(aes(image = team_logo_espn), size = 0.1) +
geom_vline(xintercept = avg_ev, linetype = "dashed") +
geom_hline(yintercept = avg_nonHr_slg, linetype = "dashed") +
labs(title = "Non-HR Slugging Percentage vs Exit Velocity by Team, 2023 MLB Season",
x = "Exit Velocity (EV)",
y = "Non-HR Slugging Percentage",
color = "Team") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_line(color = "gray", linetype = "dashed")
) +
annotate("text", x = 91, y = 0.3, label = paste("Correlation:", round(correlation_coefficient_ev_slg_noHR, 5)),
hjust = 1, vjust = 0, size = 3, color = "red")
#https://twitter.com/TotallyREALSpo1/status/1794970451715756266