Visualize PITCHf/x strikezones

Share:

Description

A suite of bivariate plots with "px" on the horizontal axis and "pz" on the vertical axis.

Usage

1
2
3
4
5
strikeFX(data, geom = "point", contour = FALSE, point.size = 3,
  point.alpha = 1/3, color = "pitch_types", fill = "des",
  layer = list(), model, model.save = TRUE, density1 = list(),
  density2 = list(), limitz = c(-2, 2, 0.5, 4.5), adjust = FALSE,
  draw_zones = TRUE, parent = FALSE, ...)

Arguments

data

PITCHf/x data to be visualized.

geom

plotting geometry. Current choices are: "point", "hex", "bin", "tile" and "raster"

contour

logical. Should contour lines be included?

point.size

Size of points (when geom="point")

point.alpha

plotting transparency parameter (when geom="point").

color

variable used to define coloring scheme.

fill

variable used to define subplot scheme (when geom="subplot2d").

layer

list of other ggplot2 (layered) modifications.

model

Either a gamObject or a call to fit a model via gam or bam. Note that the horizontal and vertical location of the pitch MUST be included as covariates named "px" and "pz", respectively. Relevant factor variables must also be included as covariates in order to produce facetted or differenced plot(s). If this option is used, the geometry must be either "hex", "tile" or "bin". If a non-valid geometry is used, the geometry will be forced to "tile".

model.save

logical. Save the fitted model? If TRUE, the relevant model object is assigned to the global environment

density1

List of length one. The name should correspond to a variable in data. The value should correspond to an (observed) value of that variable.

density2

Similar to density1. If density1 != density2, the relevant estimates are automatically differenced.

limitz

limits for horizontal and vertical axes.

adjust

logical. Should vertical locations be adjusted according to batter height?

draw_zones

logical. Should strikezones be included?

parent

is the function being called from a higher-level function? (experimental)

...

extra options passed onto geom commands

Value

Returns an object of the class used by package ggplot2 to represent plots.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
data(pitches)

strikeFX(pitches)
## Not run: 
strikeFX(pitches, layer=facet_grid(.~stand))
 #silly example on how to modify default settings and add layers
 strikeFX(pitches, color="", layer=facet_grid(s~stand))+
 geom_point(aes(x=px, y=pz, shape=pitch_types))+ #you could add color here
 geom_text(aes(x=px+0.5, y=pz, label=b))

 p <- strikeFX(pitches, geom="tile", layer=facet_grid(.~stand))
 p+theme(aspect.ratio=1)

strikeFX(pitches, geom="hex", density1=list(des="Called Strike"), density2=list(des="Ball"),
         draw_zones=FALSE, contour=TRUE, layer=facet_grid(.~stand))

noswing <- subset(pitches, des %in% c("Ball", "Called Strike"))
noswing$strike <- as.numeric(noswing$des %in% "Called Strike")
library(mgcv)
m1 <- bam(strike ~ s(px, pz, by=factor(stand)) +
               factor(stand), data=noswing, family = binomial(link='logit'))
# geom will automatically be set to 'raster'
strikeFX(noswing, model=m1, layer=facet_grid(.~stand))

m2 <- bam(strike ~ s(px, pz, by=factor(stand)) + s(px, pz, by=factor(inning_side)) +
           factor(stand) + factor(inning_side), data=noswing, family = binomial(link='logit'))
strikeFX(noswing, model=m2, density1=list(inning_side="top"),
         density2=list(inning_side="bottom"), layer=facet_grid(.~stand))

## End(Not run)