TimStats commited on
Commit
3217859
·
verified ·
1 Parent(s): 436845b

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +89 -6
app.R CHANGED
@@ -98,10 +98,29 @@ pitcher_summary <- function(game_pk,date){
98
  }
99
 
100
  break_plot <- function(game){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  ggplot(game, aes(x = HB, y = IVB, color = pitch_name)) +
102
  geom_point(size = 2) +
103
  geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
104
  geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
 
105
  labs(x = "Horizontal Break (in)", y = "Induced Vertical Break (in)",
106
  title = "Pitch Movement") +
107
  xlim(-25, 25) +
@@ -121,12 +140,31 @@ pitch_plot <- function(game, title, bhand = NULL) {
121
  # game <- game %>% filter(bhand == bhand)
122
  # }
123
  #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  ggplot(game, aes(x = px, y = pz, color = pitch_name)) +
125
  geom_point(size = 3.5) +
126
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
127
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
128
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
129
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
 
130
  labs(x = NULL, y = NULL, title = title) +
131
  xlim(-3, 3) +
132
  ylim(0.2, 4) +
@@ -201,12 +239,32 @@ pitch_plot_split <- function(game, title) {
201
  game_lhb <- game %>% filter(bhand == "L")
202
  game_rhb <- game %>% filter(bhand == "R")
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  plot_lhb <- ggplot(game_lhb, aes(x = px, y = pz, color = pitch_name)) +
205
- geom_point(size = 2) +
206
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
207
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
208
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
209
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
 
210
  labs(x = NULL, y = NULL, title = "LHB (Batter's Perspective)") +
211
  xlim(-3, 3) +
212
  ylim(0.2, 4) +
@@ -218,13 +276,15 @@ pitch_plot_split <- function(game, title) {
218
  axis.text = element_blank(),
219
  axis.ticks = element_blank()
220
  )
 
221
 
222
  plot_rhb <- ggplot(game_rhb, aes(x = px, y = pz, color = pitch_name)) +
223
- geom_point(size = 2) +
224
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
225
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
226
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
227
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
 
228
  labs(x = NULL, y = NULL, title = "RHB (Batter's Perspective)") +
229
  xlim(-3, 3) +
230
  ylim(0.2, 4) +
@@ -268,6 +328,9 @@ calculate_timstuff <- function(game) {
268
  summary_table <- function(game) {
269
  rows <- nrow(game)
270
  game <- calculate_timstuff(game)
 
 
 
271
  sumtable <- game %>%
272
  mutate(team_fielding_id = ifelse(description == "Called Strike" |
273
  description == "Swinging Strike" |
@@ -368,7 +431,6 @@ sbteamA <- sbid %>%
368
  select(teams_away_team_name)
369
  sbteamA <- distinct(sbteamA)
370
 
371
- model <- xgb.load('TimStuff2.model')
372
  # UI Definition
373
  ui <- fluidPage(
374
  theme = bs_theme(version = 5, bootswatch = "flatly"),
@@ -476,6 +538,9 @@ server <- function(input, output, session) {
476
  req(input$update1, game_data())
477
  game <- game_data() %>% filter(`Pitcher Name` == input$pitcher)
478
  game <- calculate_timstuff(game)
 
 
 
479
  result <- game %>%
480
  arrange(date) %>%
481
  group_by(pitch_name) %>%
@@ -555,13 +620,30 @@ server <- function(input, output, session) {
555
  img_grob <- textGrob("Image not available", gp = gpar(col = "red", fontsize = 20))
556
  }
557
 
558
- # Create the rolling TimStuff+ graph
559
- # Create the rolling TimStuff+ graph
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  # Create the rolling TimStuff+ graph
561
  rolling_data <- rolling_timstuff()
562
  timstuff_plot <- ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
563
  geom_line(size = 1, na.rm = TRUE) +
564
  geom_point(size = 1, na.rm = TRUE) +
 
565
  theme_minimal() +
566
  ### Workaround to not show anything but keep graph
567
  labs(title = if(any(rolling_data$pitch_number >= 5)) "5-Pitch Rolling TimStuff+" else "5-Pitch Rolling TimStuff+",
@@ -602,6 +684,7 @@ server <- function(input, output, session) {
602
  get_legend(
603
  ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
604
  geom_point(size = 5) +
 
605
  theme(legend.position = "bottom",
606
  legend.title = element_blank(),
607
  text = element_text(size = 12.5),
@@ -687,4 +770,4 @@ server <- function(input, output, session) {
687
  }
688
 
689
  # Run the application
690
- shinyApp(ui = ui, server = server)
 
98
  }
99
 
100
  break_plot <- function(game){
101
+ pitch_colors <- c(
102
+ "Four-Seam Fastball"= "#FF4136",
103
+ "Sinker"= "#FF851B",
104
+ "Cutter"= "#FFDC00",
105
+ "Changeup"= "#2ECC40",
106
+ "Slider"= "#0074D9",
107
+ "Sweeper"= "#ED68ED",
108
+ "Curveball"= "#B10DC9",
109
+ "Splitter"= "#F012BE",
110
+ "Knuckle Curve"= "#85144b",
111
+ "Slurve"= "#3D9970",
112
+ "Knuckle Ball"= "#39CCCC",
113
+ "Forkball"= "#01FF70",
114
+ "Eephus"= "#AAAAAA",
115
+ "Fastball"= "#7FDBFF",
116
+ "Slow Curve"= "#DDDDDD",
117
+ "Screwball"= "#FF69B4"
118
+ )
119
  ggplot(game, aes(x = HB, y = IVB, color = pitch_name)) +
120
  geom_point(size = 2) +
121
  geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
122
  geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
123
+ scale_color_manual(values = pitch_colors) +
124
  labs(x = "Horizontal Break (in)", y = "Induced Vertical Break (in)",
125
  title = "Pitch Movement") +
126
  xlim(-25, 25) +
 
140
  # game <- game %>% filter(bhand == bhand)
141
  # }
142
  #
143
+ pitch_colors <- c(
144
+ "Four-Seam Fastball"= "#FF4136",
145
+ "Sinker"= "#FF851B",
146
+ "Cutter"= "#FFDC00",
147
+ "Changeup"= "#2ECC40",
148
+ "Slider"= "#0074D9",
149
+ "Sweeper"= "#ED68ED",
150
+ "Curveball"= "#B10DC9",
151
+ "Splitter"= "#F012BE",
152
+ "Knuckle Curve"= "#85144b",
153
+ "Slurve"= "#3D9970",
154
+ "Knuckle Ball"= "#39CCCC",
155
+ "Forkball"= "#01FF70",
156
+ "Eephus"= "#AAAAAA",
157
+ "Fastball"= "#7FDBFF",
158
+ "Slow Curve"= "#DDDDDD",
159
+ "Screwball"= "#FF69B4"
160
+ )
161
  ggplot(game, aes(x = px, y = pz, color = pitch_name)) +
162
  geom_point(size = 3.5) +
163
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
164
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
165
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
166
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
167
+ scale_color_manual(values = pitch_colors) +
168
  labs(x = NULL, y = NULL, title = title) +
169
  xlim(-3, 3) +
170
  ylim(0.2, 4) +
 
239
  game_lhb <- game %>% filter(bhand == "L")
240
  game_rhb <- game %>% filter(bhand == "R")
241
 
242
+ pitch_colors <- c(
243
+ "Four-Seam Fastball"= "#FF4136",
244
+ "Sinker"= "#FF851B",
245
+ "Cutter"= "#FFDC00",
246
+ "Changeup"= "#2ECC40",
247
+ "Slider"= "#0074D9",
248
+ "Sweeper"= "#ED68ED",
249
+ "Curveball"= "#B10DC9",
250
+ "Splitter"= "#F012BE",
251
+ "Knuckle Curve"= "#85144b",
252
+ "Slurve"= "#3D9970",
253
+ "Knuckle Ball"= "#39CCCC",
254
+ "Forkball"= "#01FF70",
255
+ "Eephus"= "#AAAAAA",
256
+ "Fastball"= "#7FDBFF",
257
+ "Slow Curve"= "#DDDDDD",
258
+ "Screwball"= "#FF69B4"
259
+ )
260
+
261
  plot_lhb <- ggplot(game_lhb, aes(x = px, y = pz, color = pitch_name)) +
262
+ geom_point(size = 3) +
263
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
264
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
265
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
266
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
267
+ scale_color_manual(values = pitch_colors) +
268
  labs(x = NULL, y = NULL, title = "LHB (Batter's Perspective)") +
269
  xlim(-3, 3) +
270
  ylim(0.2, 4) +
 
276
  axis.text = element_blank(),
277
  axis.ticks = element_blank()
278
  )
279
+ # scale_color_manual(values = pitch_colors)
280
 
281
  plot_rhb <- ggplot(game_rhb, aes(x = px, y = pz, color = pitch_name)) +
282
+ geom_point(size = 3) +
283
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
284
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
285
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
286
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
287
+ scale_color_manual(values = pitch_colors) +
288
  labs(x = NULL, y = NULL, title = "RHB (Batter's Perspective)") +
289
  xlim(-3, 3) +
290
  ylim(0.2, 4) +
 
328
  summary_table <- function(game) {
329
  rows <- nrow(game)
330
  game <- calculate_timstuff(game)
331
+ game <- game %>%
332
+ mutate(TimStuff = ifelse(TimStuff > 150,NA,TimStuff)) %>%
333
+ mutate(TimStuff = ifelse(TimStuff < 50,NA,TimStuff))
334
  sumtable <- game %>%
335
  mutate(team_fielding_id = ifelse(description == "Called Strike" |
336
  description == "Swinging Strike" |
 
431
  select(teams_away_team_name)
432
  sbteamA <- distinct(sbteamA)
433
 
 
434
  # UI Definition
435
  ui <- fluidPage(
436
  theme = bs_theme(version = 5, bootswatch = "flatly"),
 
538
  req(input$update1, game_data())
539
  game <- game_data() %>% filter(`Pitcher Name` == input$pitcher)
540
  game <- calculate_timstuff(game)
541
+ game <- game %>%
542
+ mutate(TimStuff = ifelse(TimStuff > 150,NA,TimStuff)) %>%
543
+ mutate(TimStuff = ifelse(TimStuff < 50,NA,TimStuff))
544
  result <- game %>%
545
  arrange(date) %>%
546
  group_by(pitch_name) %>%
 
620
  img_grob <- textGrob("Image not available", gp = gpar(col = "red", fontsize = 20))
621
  }
622
 
623
+ pitch_colors <- c(
624
+ "Four-Seam Fastball"= "#FF4136",
625
+ "Sinker"= "#FF851B",
626
+ "Cutter"= "#FFDC00",
627
+ "Changeup"= "#2ECC40",
628
+ "Slider"= "#0074D9",
629
+ "Sweeper"= "#ED68ED",
630
+ "Curveball"= "#B10DC9",
631
+ "Splitter"= "#F012BE",
632
+ "Knuckle Curve"= "#85144b",
633
+ "Slurve"= "#3D9970",
634
+ "Knuckle Ball"= "#39CCCC",
635
+ "Forkball"= "#01FF70",
636
+ "Eephus"= "#AAAAAA",
637
+ "Fastball"= "#7FDBFF",
638
+ "Slow Curve"= "#DDDDDD",
639
+ "Screwball"= "#FF69B4"
640
+ )
641
  # Create the rolling TimStuff+ graph
642
  rolling_data <- rolling_timstuff()
643
  timstuff_plot <- ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
644
  geom_line(size = 1, na.rm = TRUE) +
645
  geom_point(size = 1, na.rm = TRUE) +
646
+ scale_color_manual(values = pitch_colors) +
647
  theme_minimal() +
648
  ### Workaround to not show anything but keep graph
649
  labs(title = if(any(rolling_data$pitch_number >= 5)) "5-Pitch Rolling TimStuff+" else "5-Pitch Rolling TimStuff+",
 
684
  get_legend(
685
  ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
686
  geom_point(size = 5) +
687
+ scale_color_manual(values = pitch_colors) +
688
  theme(legend.position = "bottom",
689
  legend.title = element_blank(),
690
  text = element_text(size = 12.5),
 
770
  }
771
 
772
  # Run the application
773
+ shinyApp(ui = ui, server = server)_=