Advertisement
matthewrmata

Angle and Movement Plots by Pitcher

Jul 23rd, 2016
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 7.02 KB | None | 0 0
  1. ###########################
  2. # Angle and Movement Plot #
  3. ###########################
  4.  
  5. #############
  6. # LIBRARIES #
  7. #############
  8.  
  9. library(RODBC);
  10.  
  11. #########
  12. # FILES #
  13. #########
  14.  
  15. source("Pitch_Plane_Functions.r");
  16.  
  17. ########
  18. # MAIN #
  19. ########
  20.  
  21. # Set the pitcher name
  22. First <- 'Max';
  23. Last <- 'Scherzer';
  24. Year <- '2015';
  25.  
  26. # Open the channel
  27. channel <- odbcConnect(paste("MLB",Year,sep=""));
  28.  
  29. # Set the query to find the types of pitches thrown by the pitcher
  30. Type_Query = paste("SELECT DISTINCT pitch_type FROM pitches WHERE
  31.                   ab_id IN (SELECT ab_id FROM atbats WHERE pitcher = (SELECT
  32.                   eliasid FROM players WHERE first = '",First,"' AND
  33.                   last = '",Last,"')) AND px IS NOT NULL HAVING pitch_type <> 'PO' AND
  34.                    pitch_type <> 'IN'",sep="");
  35.  
  36. # Download the pitch types
  37. Types <- sqlQuery(channel,Type_Query);
  38.  
  39. # Close the channel
  40. close(channel);
  41.  
  42. # Find the number of types of pitches as classified by PITCHf/x
  43. P_types <- Types$pitch_type;
  44. N_types <- length(P_types);
  45.  
  46. # Set an array of colors for the plots
  47. color_array <- c('red','green','blue','black','orange','yellow','brown');
  48.  
  49. # Set up arrays for storing the data
  50. Angle_Rho <- list();
  51. IP_Mvt <- list();
  52. IP_Mvt_G <- list();
  53. Speed <- list();
  54. H_Mvt <- list();
  55. V_Mvt <- list();
  56.  
  57. for (i in 1:N_types){
  58.     # Open the channel
  59.     channel <- odbcConnect(paste("MLB",Year,sep=""));
  60.     # Set the query to find the pitch data for each pitcher
  61.     Pitch_Query <- paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az, start_speed, pfx_x, pfx_z FROM pitches WHERE
  62.                           ab_id IN (SELECT ab_id FROM atbats WHERE
  63.                           pitcher = (SELECT eliasid FROM players WHERE first = '",
  64.                           First,"' AND last = '",Last,"')) AND pitch_type = '",P_types[i],"'
  65.                           AND px IS NOT NULL",sep="");
  66.     # Download the individual pitch data
  67.     PFX_Data <- sqlQuery(channel,Pitch_Query);
  68.     # Close the channel
  69.     close(channel);
  70.     # Set the number of pitches
  71.     N <- length(PFX_Data$x0);
  72.     # Set an array for the angles and the speed
  73.     Angle_Rho_Temp <- array(0,dim=N);
  74.     IP_Mvt_Temp <- array(0,dim=N);
  75.     IP_Mvt_G_Temp <- array(0,dim=N);
  76.     Speed_Temp <- PFX_Data$start_speed;
  77.     H_Mvt_Temp <- PFX_Data$pfx_x;
  78.     V_Mvt_Temp <- PFX_Data$pfx_z;
  79.     for (j in 1:N){
  80.         PITCH <- list(ax = PFX_Data$ax[j], ay = PFX_Data$ay[j], az = PFX_Data$az[j], vx0 = PFX_Data$vx0[j], vy0 = PFX_Data$vy0[j], vz0 = PFX_Data$vz0[j], x0 = PFX_Data$x0[j], y0 = PFX_Data$y0[j], z0 = PFX_Data$z0[j]);
  81.         # Find the binormal vector
  82.         B <- Binormal_Vector(PITCH);
  83.         # Find the angle from vertical of the plane
  84.         Angle_Rho_Temp[j] <- Vertical_Angle(PITCH);
  85.         # Find the movement of the pitch
  86.         IP_Mvt_Temp[j] <- IP_Movement(PITCH);
  87.         IP_Mvt_G_Temp[j] <- IP_Movement_G(PITCH);
  88.     }
  89.     Angle_Rho <- c(Angle_Rho,list(Angle_Rho_Temp));
  90.     IP_Mvt <- c(IP_Mvt,list(IP_Mvt_Temp));
  91.     IP_Mvt_G <- c(IP_Mvt_G,list(IP_Mvt_G_Temp));
  92.     Speed <- c(Speed,list(Speed_Temp));
  93.     H_Mvt <- c(H_Mvt,list(H_Mvt_Temp));
  94.     V_Mvt <- c(V_Mvt,list(V_Mvt_Temp));
  95. }
  96.  
  97. # Find the min values of each list
  98. Rho_min <- min(unlist(Angle_Rho));
  99. IP_min <- min(unlist(IP_Mvt));
  100. IP_G_min <- min(unlist(IP_Mvt_G));
  101. Speed_min <- min(unlist(Speed));
  102. H_min <- min(unlist(H_Mvt));
  103. V_min <- min(unlist(V_Mvt));
  104.  
  105. # Find the max values of each list
  106. Rho_max <- max(unlist(Angle_Rho));
  107. IP_max <- max(unlist(IP_Mvt));
  108. IP_G_max <- max(unlist(IP_Mvt_G));
  109. Speed_max <- max(unlist(Speed));
  110. H_max <- max(unlist(H_Mvt));
  111. V_max <- max(unlist(V_Mvt));
  112.  
  113. # Set the limits in x and y for the plots
  114. K <- 0.02;
  115. Rho_lim <- c(Rho_min - K*abs(Rho_max-Rho_min),Rho_max + K*abs(Rho_max-Rho_min));
  116. IP_lim <- c(IP_min - K*abs(IP_max-IP_min),IP_max + K*abs(IP_max-IP_min));
  117. IP_G_lim <- c(IP_G_min - K*abs(IP_G_max-IP_G_min),IP_G_max + K*abs(IP_G_max-IP_G_min));
  118. Speed_lim <- c(Speed_min - K*abs(Speed_max-Speed_min),Speed_max + K*abs(Speed_max-Speed_min));
  119. H_lim <- c(H_min - K*abs(H_max-H_min),H_max + K*abs(H_max-H_min));
  120. V_lim <- c(V_min - K*abs(V_max-V_min),V_max + K*abs(V_max-V_min));
  121.  
  122. # Set up the parameters for a legend
  123. col_legend <- color_array[1:N_types];
  124. pch_legend <- array(20,dim=N_types);
  125.  
  126. # For the legend, the first two values are the location of the table, and "xjust" and "yjust" set the legend to left/right and above/below this location using "0" or "1".
  127.  
  128. windows();
  129.  
  130. plot(0,0,type='n',xlim=Speed_lim,ylim=Rho_lim,xaxs='i',yaxs='i',xlab='Speed (MPH)',ylab='Angle From Vertical (Degrees)');
  131.  
  132. # Speed vs. Angle_Rho
  133. plot(0,0,type='n',xlim=Speed_lim,ylim=Rho_lim,xaxs='i',yaxs='i',xlab='Speed (MPH)',ylab='Angle From Vertical (Degrees)');
  134. for (i in 1:N_types){
  135.     par(new=TRUE);
  136.     plot(Speed[[i]],Angle_Rho[[i]],pch=20,xlim=Speed_lim,ylim=Rho_lim,col=color_array[i],xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  137. }
  138. legend(1.01*Speed_min,0.99*Rho_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
  139. title(main=paste(First," ",Last," (",Year,"): Speed vs. Angle From Vertical",sep=""));
  140. par(new=FALSE);
  141.  
  142. windows();
  143.  
  144. # H_Mvt vs. V_Mvt
  145. plot(0,0,type='n',xlim=H_lim,ylim=V_lim,xaxs='i',yaxs='i',xlab='Horizontal Movement (Inches)',ylab='Vertical Movement (Inches)');
  146. for (i in 1:N_types){
  147.     par(new=TRUE);
  148.     plot(H_Mvt[[i]],V_Mvt[[i]],pch=20,xlim=H_lim,ylim=V_lim,col=color_array[i],xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  149. }
  150. legend(1.01*H_min,1.01*V_min,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=0);
  151. title(main=paste(First," ",Last," (",Year,"): Horizontal vs. Vertical Movement",sep=""));
  152. par(new=FALSE);
  153.  
  154. windows();
  155.  
  156. # IP_Mvt vs. Angle_Rho
  157. plot(0,0,type='n',xlim=IP_lim,ylim=Rho_lim,xaxs='i',yaxs='i',xlab='In-Plane Movement (Inches)',ylab='Angle From Vertical (Degrees)');
  158. for (i in 1:N_types){
  159.     par(new=TRUE);
  160.     plot(IP_Mvt[[i]],Angle_Rho[[i]],pch=20,xlim=IP_lim,ylim=Rho_lim,col=color_array[i],xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  161. }
  162. legend(1.01*IP_min,0.99*Rho_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
  163. title(main=paste(First," ",Last," (",Year,"): In-Plane Mvt. vs. Angle From Vertical",sep=""));
  164. par(new=FALSE);
  165.  
  166. windows();
  167.  
  168. # IP_Mvt vs. Speed
  169. plot(0,0,type='n',xlim=IP_lim,ylim=Speed_lim,xaxs='i',yaxs='i',xlab='In-Plane Movement (Inches)',ylab='Speed (MPH)');
  170. for (i in 1:N_types){
  171.     par(new=TRUE);
  172.     plot(IP_Mvt[[i]],Speed[[i]],pch=20,xlim=IP_lim,ylim=Speed_lim,col=color_array[i],xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  173. }
  174. legend(1.01*IP_min,0.99*Speed_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
  175. title(main=paste(First," ",Last," (",Year,"): In-Plane Movement vs. Speed",sep=""));
  176. par(new=FALSE);
  177.  
  178. windows();
  179.  
  180. # IP_Mvt_G vs. Speed
  181. plot(0,0,type='n',xlim=IP_G_lim,ylim=Speed_lim,xaxs='i',yaxs='i',xlab='In-Plane Movement w/ Gravity (Inches)',ylab='Speed (MPH)');
  182. for (i in 1:N_types){
  183.     par(new=TRUE);
  184.     plot(IP_Mvt_G[[i]],Speed[[i]],pch=20,xlim=IP_G_lim,ylim=Speed_lim,col=color_array[i],xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  185. }
  186. legend(1.01*IP_G_min,0.99*Speed_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
  187. title(main=paste(First," ",Last," (",Year,"): In-Plane Mvt. + Gravity vs. Speed",sep=""));
  188. par(new=FALSE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement