Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
parkType <- pk_factors %>%
mutate( TYPE = ifelse( test = PK_FACTOR > 1, yes = "Batters"
, no = ifelse( test = PK_FACTOR < 1, yes = "Pitchers"
, no = "Neutral"
))) %>%
group_by( YEAR, TYPE ) %>%
summarise( n = n() )
( ggplot()
+ geom_point( data = parkType
, aes( x = YEAR
, y = n
, col = TYPE
)
)
+ geom_smooth( data = parkType[parkType$TYPE == "Batters",]
, aes( x = YEAR
, y = n
, col = TYPE
)
, se = F
)
+ geom_smooth( data = parkType[parkType$TYPE == "Pitchers",]
, aes( x = YEAR
, y = n
, col = TYPE
)
, se = F
)
+ scale_color_manual( values = c("goldenrod", "dodgerblue") )
+ labs( x = "Year"
, y = "Parks"
, col = "Park Type"
)
)
yankeeStad <- pk_factors %>%
filter( PARK_ID == 'NYC16' ) %>%
mutate( PCT = ( PK_FACTOR/lag(x = PK_FACTOR, n = 1 ) - 1 ) * 100
, SGN = ifelse( test = is.na(PCT), yes = 'UNSGN'
, no = ifelse( test = PCT > 1, yes = "POS"
, no = "NEG" ))
, PCT = ifelse( test = is.na( x = PCT ), yes = PK_FACTOR, no = PCT )
, PCT = round( x = PCT, digits = 2 )
)
( ggplot()
+ geom_hline( aes( yintercept = 1 )
, col = "goldenrod"
, size = 0.5
)
+ geom_point( data = yankeeStad
, aes( x = YEAR
, y = PK_FACTOR
, col = SGN
, shape = SGN
, fill = SGN
)
, alpha = 0.8
, size = 2.5
)
+ geom_text( data = yankeeStad
, aes( x = YEAR - 0.5
, y = PK_FACTOR
, col = SGN
, label = PCT
)
, size = 3
)
+ geom_line( data = yankeeStad
, aes( x = YEAR
, y = PK_FACTOR
, group = 1
)
, size = 0.1
, alpha = 0.15
)
+ labs( x = "Year"
, y = "Park Factor"
)
+ scale_shape_manual( values = c( 25, 24, 16 ) )
+ scale_fill_manual( values = c( "red", "green4", "blue" ) )
+ theme( legend.position = "none" )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.