Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################
- # Angle and Movement Plot #
- ###########################
- #############
- # LIBRARIES #
- #############
- library(RODBC);
- #########
- # FILES #
- #########
- source("Pitch_Plane_Functions.r");
- ########
- # MAIN #
- ########
- # Set the pitcher name
- First <- 'Max';
- Last <- 'Scherzer';
- Year <- '2015';
- # Open the channel
- channel <- odbcConnect(paste("MLB",Year,sep=""));
- # Set the query to find the types of pitches thrown by the pitcher
- Type_Query = paste("SELECT DISTINCT pitch_type FROM pitches WHERE
- ab_id IN (SELECT ab_id FROM atbats WHERE pitcher = (SELECT
- eliasid FROM players WHERE first = '",First,"' AND
- last = '",Last,"')) AND px IS NOT NULL HAVING pitch_type <> 'PO' AND
- pitch_type <> 'IN'",sep="");
- # Download the pitch types
- Types <- sqlQuery(channel,Type_Query);
- # Close the channel
- close(channel);
- # Find the number of types of pitches as classified by PITCHf/x
- P_types <- Types$pitch_type;
- N_types <- length(P_types);
- # Set an array of colors for the plots
- color_array <- c('red','green','blue','black','orange','yellow','brown');
- # Set up arrays for storing the data
- Angle_Rho <- list();
- IP_Mvt <- list();
- IP_Mvt_G <- list();
- Speed <- list();
- H_Mvt <- list();
- V_Mvt <- list();
- for (i in 1:N_types){
- # Open the channel
- channel <- odbcConnect(paste("MLB",Year,sep=""));
- # Set the query to find the pitch data for each pitcher
- Pitch_Query <- paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az, start_speed, pfx_x, pfx_z FROM pitches WHERE
- ab_id IN (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- First,"' AND last = '",Last,"')) AND pitch_type = '",P_types[i],"'
- AND px IS NOT NULL",sep="");
- # Download the individual pitch data
- PFX_Data <- sqlQuery(channel,Pitch_Query);
- # Close the channel
- close(channel);
- # Set the number of pitches
- N <- length(PFX_Data$x0);
- # Set an array for the angles and the speed
- Angle_Rho_Temp <- array(0,dim=N);
- IP_Mvt_Temp <- array(0,dim=N);
- IP_Mvt_G_Temp <- array(0,dim=N);
- Speed_Temp <- PFX_Data$start_speed;
- H_Mvt_Temp <- PFX_Data$pfx_x;
- V_Mvt_Temp <- PFX_Data$pfx_z;
- for (j in 1:N){
- 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]);
- # Find the binormal vector
- B <- Binormal_Vector(PITCH);
- # Find the angle from vertical of the plane
- Angle_Rho_Temp[j] <- Vertical_Angle(PITCH);
- # Find the movement of the pitch
- IP_Mvt_Temp[j] <- IP_Movement(PITCH);
- IP_Mvt_G_Temp[j] <- IP_Movement_G(PITCH);
- }
- Angle_Rho <- c(Angle_Rho,list(Angle_Rho_Temp));
- IP_Mvt <- c(IP_Mvt,list(IP_Mvt_Temp));
- IP_Mvt_G <- c(IP_Mvt_G,list(IP_Mvt_G_Temp));
- Speed <- c(Speed,list(Speed_Temp));
- H_Mvt <- c(H_Mvt,list(H_Mvt_Temp));
- V_Mvt <- c(V_Mvt,list(V_Mvt_Temp));
- }
- # Find the min values of each list
- Rho_min <- min(unlist(Angle_Rho));
- IP_min <- min(unlist(IP_Mvt));
- IP_G_min <- min(unlist(IP_Mvt_G));
- Speed_min <- min(unlist(Speed));
- H_min <- min(unlist(H_Mvt));
- V_min <- min(unlist(V_Mvt));
- # Find the max values of each list
- Rho_max <- max(unlist(Angle_Rho));
- IP_max <- max(unlist(IP_Mvt));
- IP_G_max <- max(unlist(IP_Mvt_G));
- Speed_max <- max(unlist(Speed));
- H_max <- max(unlist(H_Mvt));
- V_max <- max(unlist(V_Mvt));
- # Set the limits in x and y for the plots
- K <- 0.02;
- Rho_lim <- c(Rho_min - K*abs(Rho_max-Rho_min),Rho_max + K*abs(Rho_max-Rho_min));
- IP_lim <- c(IP_min - K*abs(IP_max-IP_min),IP_max + K*abs(IP_max-IP_min));
- 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));
- Speed_lim <- c(Speed_min - K*abs(Speed_max-Speed_min),Speed_max + K*abs(Speed_max-Speed_min));
- H_lim <- c(H_min - K*abs(H_max-H_min),H_max + K*abs(H_max-H_min));
- V_lim <- c(V_min - K*abs(V_max-V_min),V_max + K*abs(V_max-V_min));
- # Set up the parameters for a legend
- col_legend <- color_array[1:N_types];
- pch_legend <- array(20,dim=N_types);
- # 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".
- windows();
- plot(0,0,type='n',xlim=Speed_lim,ylim=Rho_lim,xaxs='i',yaxs='i',xlab='Speed (MPH)',ylab='Angle From Vertical (Degrees)');
- # Speed vs. Angle_Rho
- plot(0,0,type='n',xlim=Speed_lim,ylim=Rho_lim,xaxs='i',yaxs='i',xlab='Speed (MPH)',ylab='Angle From Vertical (Degrees)');
- for (i in 1:N_types){
- par(new=TRUE);
- 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='');
- }
- legend(1.01*Speed_min,0.99*Rho_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
- title(main=paste(First," ",Last," (",Year,"): Speed vs. Angle From Vertical",sep=""));
- par(new=FALSE);
- windows();
- # H_Mvt vs. V_Mvt
- plot(0,0,type='n',xlim=H_lim,ylim=V_lim,xaxs='i',yaxs='i',xlab='Horizontal Movement (Inches)',ylab='Vertical Movement (Inches)');
- for (i in 1:N_types){
- par(new=TRUE);
- 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='');
- }
- legend(1.01*H_min,1.01*V_min,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=0);
- title(main=paste(First," ",Last," (",Year,"): Horizontal vs. Vertical Movement",sep=""));
- par(new=FALSE);
- windows();
- # IP_Mvt vs. Angle_Rho
- 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)');
- for (i in 1:N_types){
- par(new=TRUE);
- 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='');
- }
- legend(1.01*IP_min,0.99*Rho_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
- title(main=paste(First," ",Last," (",Year,"): In-Plane Mvt. vs. Angle From Vertical",sep=""));
- par(new=FALSE);
- windows();
- # IP_Mvt vs. Speed
- plot(0,0,type='n',xlim=IP_lim,ylim=Speed_lim,xaxs='i',yaxs='i',xlab='In-Plane Movement (Inches)',ylab='Speed (MPH)');
- for (i in 1:N_types){
- par(new=TRUE);
- 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='');
- }
- legend(1.01*IP_min,0.99*Speed_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
- title(main=paste(First," ",Last," (",Year,"): In-Plane Movement vs. Speed",sep=""));
- par(new=FALSE);
- windows();
- # IP_Mvt_G vs. Speed
- 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)');
- for (i in 1:N_types){
- par(new=TRUE);
- 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='');
- }
- legend(1.01*IP_G_min,0.99*Speed_max,P_types,col=col_legend,pch=pch_legend,xjust=0,yjust=1);
- title(main=paste(First," ",Last," (",Year,"): In-Plane Mvt. + Gravity vs. Speed",sep=""));
- par(new=FALSE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement