Created
November 9, 2016 22:08
-
-
Save andresAlvarado/70f6c05c1404480384c13386da29d25c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library('ggplot2') | |
# Dataset column names and classes. | |
l_colnames = c( 'game_no','stadium', 'team', 'x_cord', 'y_cord', 'desc' ) | |
l_colClasses = c( 'numeric', 'character', 'character', 'numeric', 'numeric', 'character' ) | |
# Load the dataset. | |
hip_data <- read.csv( file = 'hitsPerGame.csv' | |
, header = F | |
, col.names = l_colnames | |
, colClasses = l_colClasses | |
, na.strings = '' | |
, stringsAsFactors = T | |
) | |
# Stadium specs. | |
x_hp = 125 # X coordinate of the Homeplate. | |
y_hp = 43 # Y coordinate of the Homeplate. | |
f_len = 150 # Length of the foul line. | |
b_len = 45 # Length between bases. | |
y_max = 250 # Max y value. | |
f_angl = sqrt(2)/2 # Angle for foul lines and bases | |
# Remove errors. | |
hip_data <- hip_data[ hip_data$desc != 'Error', ] | |
# Flip y coordinate of the batted balls. | |
hip_data$y_cord <- -( hip_data$y_cord - y_max ) | |
# Starting and ending coordinates of foul lines. | |
d_bounds <- data.frame( x = rep( x = x_hp, 2 ) | |
, y = rep( x = y_hp, 2 ) | |
, xend = c( x_hp + f_angl * f_len, x_hp - f_angl * f_len ) | |
, yend = c( y_hp + f_angl * f_len, y_hp + f_angl * f_len ) | |
) | |
# Starting and ending coordinates of foul lines. | |
d_bases <- data.frame( x = c( x_hp, x_hp + f_angl * b_len | |
, x_hp, x_hp - f_angl * b_len | |
, x_hp | |
) | |
, y = c( y_hp, y_hp + f_angl * b_len | |
, y_hp + sqrt( 2 * b_len^2) | |
, y_hp + f_angl * b_len | |
, y_hp | |
) | |
) | |
# Create plot. | |
( ggplot() | |
+ geom_segment( data = d_bounds | |
, aes( x = x | |
, y = y | |
, xend = xend | |
, yend = yend | |
) | |
, color = 'white' | |
, size = 1.1 | |
) | |
+ geom_path( data = d_bases | |
, aes( x = x | |
, y = y | |
) | |
, color = 'white' | |
, size = 1.1 | |
) | |
+ geom_point( data = hip_data | |
, aes( x = x_cord | |
, y = y_cord | |
, color = team | |
) | |
, size = 1.2 | |
) | |
+ facet_wrap( ~stadium ) | |
+ coord_equal() | |
+ labs( x = 'X' | |
, y = 'Y' | |
, title = 'Batted Balls in Play - WS 2016' | |
) | |
+ scale_color_manual( values = c('royalblue3', 'firebrick1' ) | |
, guide = guide_legend( title = 'Team' ) | |
) | |
+ theme( panel.grid = element_blank() | |
, panel.background = element_rect( fill = '#ace456' ) | |
, strip.background = element_rect( fill = '#5d6c93' ) | |
, strip.text = element_text( color = 'white' ) | |
, axis.text = element_text( size = 8 ) | |
, axis.title = element_text( size = 8 ) | |
, plot.title = element_text( size = 11 ) | |
, legend.title = element_text( size = 10 ) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment