---
title: "mlbgameday: Plotting Pitches"
author: "Kris Eberwein"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{mlbgameday: Plotting Pitches}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r setup, include=TRUE}
```

The package is primarily a data package, and has no native plotting tools. However, there are several plotting options available by leveraging one of the R language's excellent plotting libraries.

## Example Data

For all of the following examples, we will use The pitch data for Jake Arrieta's no-hitter, which occurred on April 21, 2016.


```{r}
library(mlbgameday)
library(dplyr)

# Grap some Gameday data. We're specifically looking for Jake Arrieta's no-hitter.
gamedat <- get_payload(start = "2016-04-21", end = "2016-04-21")

# Subset that atbat table to only Arrieta's pitches and join it with the pitch table.
pitches <- inner_join(gamedat$pitch, gamedat$atbat, by = c("num", "url")) %>%
    subset(pitcher_name == "Jake Arrieta")

```

## Ggplot2

The ggplot2 package can be used stand-alone, or in conjunction with Carson Silvert's [pitchRx](https://github.com/cpsievert/pitchRx) package, which has additional visualization offerings that are based on ggplot2.

```{r}
library(ggplot2)

# basic example
ggplot() +
    geom_point(data=pitches, aes(x=px, y=pz, shape=type, col=pitch_type)) +
    coord_equal() + geom_path(aes(x, y), data = mlbgameday::kzone)

```

## Batting Stance

Using the same simple ggplot example, we can use `facet_grid(. ~ stand)` to segment the pitches thrown to right-handers from those thrown to left-handers.

```{r, fig.show='hold'}

# basic example with stand.
ggplot() +
    geom_point(data=pitches, aes(x=px, y=pz, shape=type, col=pitch_type)) +
    facet_grid(. ~ stand) + coord_equal() +
    geom_path(aes(x, y), data =  mlbgameday::kzone)


```


## PitchRx and ggplot2

For this visualization, we will subset the data to only pitches that the umpire called a strike. This will give us location densities of all called strikes in that game with a view from the umpire's prospective.

```{r}
# Subset the data to only called strikes.
strikes <- subset(pitches, des == "Called Strike")

library(pitchRx)
library(viridis)
pitchRx::strikeFX(pitches, geom = "tile") + 
    facet_grid(pitcher_name ~ stand) +
    coord_equal() +
    theme_bw() +
    scale_fill_viridis()

```

## Plotly

The plotly package is the "go-to" package for many who wish to create interactive visualizations or Shiny applications.

```{r}
library(plotly)
plot_ly(pitches, x = ~px, y = ~pz, mode = 'markers', 
        color = ~pitch_type, marker = list(size = 7, line = list(color = ~pitch_type, width = 1)),
        text = ~paste("Pitch Type: ", pitch_type)) %>% 
    layout(title = "Anatomy of a No-Hitter", xaxis = list(zeroline=F), yaxis = list(zeroline=F)) %>%
    # Draw a box for the strikezone. 
    layout(shapes = list(type = "rect", fillcolor = "blue", 
                         line = list(color = "blue"), opacity = 0.2,
                         x0 = -0.95, x1 = 0.95, xref = "x",
                         y0 = 3.5, y1 = 1.6, yref = "y"))

```