TimStats commited on
Commit
942445b
·
verified ·
1 Parent(s): de16f26

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +20 -21
app.R CHANGED
@@ -26,7 +26,7 @@ pitch_type_lookup <- data.frame(
26
 
27
  break_plot_Szn <- function(game,data1,sdate,edate) {
28
  game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
29
-
30
  title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
31
  # Get pitcher's handedness
32
  pitcher_hand <- unique(game$phand)
@@ -64,19 +64,14 @@ break_plot_Szn <- function(game,data1,sdate,edate) {
64
  angle_radians <- angle_degrees * (pi / 180)
65
 
66
  # Legend location based on handedness
67
- if(pitcher_hand == "L") {
68
  leg <- c(0.08, .22)
69
  factor <- -1
70
- if(angle_degrees < 0){
71
- leg <- c(0.92, .22)
72
- }
73
  } else {
74
  leg <- c(0.92, .22)
75
  factor <- 1
76
- if(angle_degrees < 0){
77
- leg <- c(0.08, .22)
78
- }
79
  }
 
80
  # Calculate the endpoint coordinates
81
  x_end <- 50 * cos(angle_radians) * factor
82
  y_end <- 50 * sin(angle_radians)
@@ -92,11 +87,15 @@ break_plot_Szn <- function(game,data1,sdate,edate) {
92
  game_pitches <- unique(game$pitch_abbr)
93
 
94
  # Filter season data for the current arm angle, handedness, and only pitches in the game
95
- arm_angle_data <- data1 %>%
96
- filter(abs(round(arm_angle) - angle_degrees) <= 2,
97
- phand == pitcher_hand) %>%
98
- left_join(pitch_type_lookup, by = c("pitch_name")) %>%
99
- filter(pitch_abbr %in% game_pitches)
 
 
 
 
100
 
101
  ggplot(game, aes(x = HB, y = IVB)) +
102
  # Base layers
@@ -173,6 +172,7 @@ break_plot_Szn <- function(game,data1,sdate,edate) {
173
  }
174
  break_plot_tot <- function(game,data1,sdate,edate) {
175
  game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
 
176
  title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
177
  # Get pitcher's handedness
178
  pitcher_hand <- unique(game$phand)
@@ -213,15 +213,9 @@ break_plot_tot <- function(game,data1,sdate,edate) {
213
  if(pitcher_hand == "L") {
214
  leg <- c(0.08, .22)
215
  factor <- -1
216
- if(angle_degrees < 0){
217
- leg <- c(0.92, .22)
218
- }
219
  } else {
220
  leg <- c(0.92, .22)
221
  factor <- 1
222
- if(angle_degrees < 0){
223
- leg <- c(0.08, .22)
224
- }
225
  }
226
 
227
  # Calculate the endpoint coordinates
@@ -240,11 +234,16 @@ break_plot_tot <- function(game,data1,sdate,edate) {
240
 
241
  # Filter season data for the current arm angle, handedness, and only pitches in the game
242
  arm_angle_data <- data1 %>%
243
- filter(abs(round(arm_angle) - angle_degrees) <= 2,
244
- phand == pitcher_hand) %>%
 
 
245
  left_join(pitch_type_lookup, by = c("pitch_name")) %>%
 
 
246
  filter(pitch_abbr %in% game_pitches)
247
 
 
248
  ggplot(game, aes(x = HB, y = IVB)) +
249
  # Base layers
250
  geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
 
26
 
27
  break_plot_Szn <- function(game,data1,sdate,edate) {
28
  game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
29
+ game <- game %>% filter(!is.na(pitch_abbr))
30
  title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
31
  # Get pitcher's handedness
32
  pitcher_hand <- unique(game$phand)
 
64
  angle_radians <- angle_degrees * (pi / 180)
65
 
66
  # Legend location based on handedness
67
+ if(pitcher_hand == "L") {
68
  leg <- c(0.08, .22)
69
  factor <- -1
 
 
 
70
  } else {
71
  leg <- c(0.92, .22)
72
  factor <- 1
 
 
 
73
  }
74
+
75
  # Calculate the endpoint coordinates
76
  x_end <- 50 * cos(angle_radians) * factor
77
  y_end <- 50 * sin(angle_radians)
 
87
  game_pitches <- unique(game$pitch_abbr)
88
 
89
  # Filter season data for the current arm angle, handedness, and only pitches in the game
90
+ arm_angle_data <- data1 %>%
91
+ # First remove NAs from relevant columns
92
+ filter(!is.na(arm_angle), !is.na(angle_degrees), !is.na(phand), !is.na(pitcher_hand), !is.na(pitch_name)) %>%
93
+ # Then proceed with your original filtering
94
+ filter(abs(round(arm_angle) - angle_degrees) <= 2, phand == pitcher_hand) %>%
95
+ left_join(pitch_type_lookup, by = c("pitch_name")) %>%
96
+ # Remove NAs that might have been introduced by the join
97
+ filter(!is.na(pitch_abbr)) %>%
98
+ filter(pitch_abbr %in% game_pitches)
99
 
100
  ggplot(game, aes(x = HB, y = IVB)) +
101
  # Base layers
 
172
  }
173
  break_plot_tot <- function(game,data1,sdate,edate) {
174
  game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
175
+ game <- game %>% filter(!is.na(pitch_abbr))
176
  title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
177
  # Get pitcher's handedness
178
  pitcher_hand <- unique(game$phand)
 
213
  if(pitcher_hand == "L") {
214
  leg <- c(0.08, .22)
215
  factor <- -1
 
 
 
216
  } else {
217
  leg <- c(0.92, .22)
218
  factor <- 1
 
 
 
219
  }
220
 
221
  # Calculate the endpoint coordinates
 
234
 
235
  # Filter season data for the current arm angle, handedness, and only pitches in the game
236
  arm_angle_data <- data1 %>%
237
+ # First remove NAs from relevant columns
238
+ filter(!is.na(arm_angle), !is.na(angle_degrees), !is.na(phand), !is.na(pitcher_hand), !is.na(pitch_name)) %>%
239
+ # Then proceed with your original filtering
240
+ filter(abs(round(arm_angle) - angle_degrees) <= 2, phand == pitcher_hand) %>%
241
  left_join(pitch_type_lookup, by = c("pitch_name")) %>%
242
+ # Remove NAs that might have been introduced by the join
243
+ filter(!is.na(pitch_abbr)) %>%
244
  filter(pitch_abbr %in% game_pitches)
245
 
246
+ game <- game %>% filter(!is.na(pitch_abbr))
247
  ggplot(game, aes(x = HB, y = IVB)) +
248
  # Base layers
249
  geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +