R Markdown

## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.2.5
## ✔ tibble  2.0.1     ✔ dplyr   0.7.8
## ✔ tidyr   0.8.2     ✔ stringr 1.3.1
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## This is mgcv 1.8-26. For overview type 'help("mgcv-package")'.
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
##    Pitch_Location Num_Pitches Percent_Location Swing_And_Misses
## 1               1          11            8.21%                2
## 2               2           3            2.24%                2
## 3               4          13            9.70%                5
## 4               5           5            3.73%                0
## 5               6           3            2.24%                0
## 6               7          31           23.13%                4
## 7               8          10            7.46%                1
## 8               9           3            2.24%                0
## 9              10           1            0.75%                0
## 10             14           1            0.75%                0
## 11             15           1            0.75%                0
## 12             16           4            2.99%                0
## 13             17          11            8.21%                0
## 14             18          14           10.45%                1
## 15             19          18           13.43%                2
## 16             20           4            2.99%                0
## 17             21           1            0.75%                0
## 18             NA          NA                                NA
##    Percent_Swing_Miss
## 1              18.18%
## 2              66.67%
## 3              38.46%
## 4               0.00%
## 5               0.00%
## 6              12.90%
## 7              10.00%
## 8               0.00%
## 9               0.00%
## 10              0.00%
## 11              0.00%
## 12              0.00%
## 13              0.00%
## 14              7.14%
## 15             11.11%
## 16              0.00%
## 17              0.00%
## 18
## defining the strike zone in a function
getzone <- function() {
  topKzone <- 3.5
  botKzone <- 1.6
  inKzone <- -.95
  outKzone <- .95 
  data.frame(
    x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
    y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
  )
}

plate_x = seq(-1.5, 1.5, length.out = 50)
plate_z = seq(0.5, 5, length.out = 50)

#ggplot(getzone(), aes(x,y)) 

r} ###load color library library(RColorBrewer) library(png)

make heat map colors

brewer.pal(11, “RdYlBu”) buylrd <- rev(brewer.pal(11,“RdYlBu”)

png(file=“WestSmoothRHB.png”, height=550, width=450) smoothScatter(pitches\(pz[pitches\)stand==“R”] ~ pitches\(px[pitches\)stand==“R”], nbin=1000, colramp = colorRampPalette(c(buylrd)), nrpoints=Inf, pch=“”, cex=.7, transformation = function(x) x^.6, col=“black”, main=“Joe West RHB Pitch Location”, xlab=“Horizontal Location”, ylab=“Vertical Location”, xlim=c(-4,4), ylim=c(-2,8)) rect(-zW, zBot, zW, zTop, border=“black”, lty=“dotted”, lwd=3) abline(h=0, lwd=3) dev.off()

png(file=“WestSmoothLHB.png”, height=550, width=450) smoothScatter(pitches\(pz[pitches\)stand==“L”] ~ pitches\(px[pitches\)stand==“L”], nbin=1000, colramp = colorRampPalette(c(buylrd)), nrpoints=Inf, pch=“”, cex=.7, transformation = function(x) x^.6, col=“black”, main=“Joe West LHB Pitch Location”, xlab=“Horizontal Location”, ylab=“Vertical Location”, xlim=c(-4,4), ylim=c(-2,8)) rect(-zW, zBot, zW, zTop, border=“black”, lty=“dotted”, lwd=3) abline(h=0, lwd=3) dev.off()


{r}
ggplot()+
  geom_path(data = data, )+
  coord_equal()+
  xlab("feet from home plate")+
  ylab("feet above the ground")+
  geom_point(data = data)+
  scale_size(range = c(0.005,2.5))+
  scale_color_manual(values = c('black','blue','red','yellow','green'))+
  geom_text(data = data)+
  xlim(-2,2)+
  ylim(0,4.5)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
x1 <- c(-.95,.95,.95,-.95,-.95)
z1 <- c(1.6,1.6,3.5,3.5,1.6)

#store in dataframe
sz <- data_frame(x1,z1) 
## Warning: `data_frame()` is deprecated, use `tibble()`.
## This warning is displayed once per session.
## heat map of pitch location percentage 

p <- plot_ly(
  x = c(-2, -1, 0, 1, 2), 
  y = c(.5, 1.5, 2.5, 3.5, 4.5), 
  z = matrix(c(0, 8, 2, 3, 0, 
               9, 15, 6, 4, 1, 
               12, 10, 4, 3, 1, 
               6, 4, 4, 1, 0,
               0, 2, 2, 2,0), nrow = 5, ncol = 5), 
  
  type = "contour" ,
   colorscale = 'Jet',
  autocontour = F,
  contours = list(
    start = 0,
    end = 15,
    size = 2),
  xlab= "Horizontal",
  ylab= "vertical"
) %>%
layout(
    title = "Heat Map of Pitch Location Percentage") %>%
  add_segments(x = -1.5, xend = 1.5, y = 1, yend = 1) %>%
  add_segments(x = -1.5, xend = 1.5, y = 4, yend = 4) %>%
  add_segments(x = -1.5, xend = -1.5, y = 1, yend = 4) %>%
  add_segments(x = 1.5, xend = 1.5, y = 1, yend = 4)

p
## Warning: 'contour' objects don't have these attributes: 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'z', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'transpose', 'xtype', 'ytype', 'zhoverformat', 'connectgaps', 'fillcolor', 'autocontour', 'ncontours', 'contours', 'line', 'zauto', 'zmin', 'zmax', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'zsrc', 'xsrc', 'ysrc', 'textsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## heat map of swing and miss percentage 



p1 <- plot_ly(
  x = c(-2, -1, 0, 1, 2), 
  y = c(.5, 1.5, 2.5, 3.5, 4.5), 
  z = matrix(c(0, 9.09, 0, 0, 0, 
               3.85, 12.20, 31.25, 16.67, 50, 
               0, 10.71, 0, 44.44, 0, 
               5.56, 33.33, 0, 33.33, 0,
               0, 0, 0, 0, 0), nrow = 5, ncol = 5), 
  
  type = "contour" ,
   colorscale = 'Jet',
  autocontour = F,
  contours = list(
    start = 0,
    end = 50,
    size = 5),
  xlab= "Horizontal",
  ylab= "vertical"
) %>%
  layout(
    title = "Heat Map of Swing and Miss Percentage") %>%
  add_segments(x = -1.5, xend = 1.5, y = 1, yend = 1) %>%
  add_segments(x = -1.5, xend = 1.5, y = 4, yend = 4) %>%
  add_segments(x = -1.5, xend = -1.5, y = 1, yend = 4) %>%
  add_segments(x = 1.5, xend = 1.5, y = 1, yend = 4)
  
  

p1
## Warning: 'contour' objects don't have these attributes: 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'z', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'transpose', 'xtype', 'ytype', 'zhoverformat', 'connectgaps', 'fillcolor', 'autocontour', 'ncontours', 'contours', 'line', 'zauto', 'zmin', 'zmax', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'zsrc', 'xsrc', 'ysrc', 'textsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'z', 'colorscale', 'autocontour', 'contours', 'xlab', 'ylab'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'