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:

              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.

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 is to create a text variable Play — this won’t be used by ggplot2, but will be used by to create interactive labels for the point.

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") +
    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.


Uploading the graph to Plotly

One can set up a free account on 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 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 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.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: