Constructing a Win Probability Graph Using plotly

As some of you might know, Plotly is an attractive environment for creating interactive graphics. There is a plotly package in R that allows you to create graphs using this language. Also it is easy to convert ggplot2 graphics into plotly objects. Here I will illustrate constructing a win probability graph using Fangraphs data.

The Data

We can’t forget the exciting Game 7 in last year’s World Series between the Cubs and Indians. Fangraphs has a nice graph that shows how the Indians win probability changed through the innings. One nice interactive feature of this graph is that by moving the mouse on the graph, the interested fan can see which plays impacted the large changes in the win probability. Fangraphs shows a table of the plays and associated win probabilities — I download this data and save it in the file “WSGame7.csv”.

You can see the data file here. You can download the data into the local directory using the following code:

library(curl)
download.file("https://gist.githubusercontent.com/bayesball/85b2ea6608776b1b97a22b9d4af4f622/raw/8bc25f2aa998dec707e62aa1178de4eed8b3e1d4/WSGame7.csv", 
              destfile = "WSGame7.csv", method = "curl")

A Little Data Prep

To get the data ready for graphing, I do a little work. I add a new variable Play_Number and convert the win probability variable to numeric.

library(readr)
library(stringr)
d <- read_csv("WSGame7.csv")
d$Play_Number <- 1:dim(d)[1]
d$WE  <- as.numeric(str_replace(d$WE, "%", ""))

Using ggplot2

I use ggplot2 to construct the plot. One new thing that I’ll need for plot.ly is to create a text variable Play — this won’t be used by ggplot2, but will be used by plot.ly to create interactive labels for the point.

library(ggplot2)
p <- ggplot(d, aes(Play_Number, WE)) + 
  geom_point(aes(text = Play), size=2) + 
  geom_line() +
  ylim(0, 100) + 
  ggtitle("2016 World Series Game 7") +
  ylab("Probability Indians Win") +
  geom_hline(yintercept = 50, color="red") +
  theme(
    plot.title = element_text(
      colour = "blue",
      size = 18
    )
  )

Here is the static version of this graph.

Convert to plotly

The function ggplotly will convert the ggplot2 to plotly and display the interactive graph in the R plot window.

ggplotly(p)

Uploading the graph to Plotly

One can set up a free account on plot.ly and upload your graphs for public viewing. This is easy to do (you insert your own username and api key below):

Sys.setenv("plotly_username"= "YOUR ID")
Sys.setenv("plotly_api_key"="YOUR KEY")
plotly_POST(p, filename = "2016ws_game7")

Here is the interactive version of my graph. (By the way, you don’t need an account with Plot.ly to see the graph.) By moving your mouse over the points, you’ll see the individual plays that modified the Indians win probability through the game.

For example, I notice the big Indians play (Davis’ home run) that increased the win probability to over 0.5.

And the big Zobrist double that put the Cubs up for good in extra winnings.

Looking Ahead

My experience with plot.ly is a bit limited. But I can see the attractiveness of adding interactive features to a graph and it appears easy to convert my ggplot2 scripts to plotly. I encourage you to explore this graphics system.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: