4.3 plotly

Previously, we created labels for a handful of players’ names. If we wanted to create labels for every player’s name, then our plot would quickly become overwhelming and all information contained in the graph would be lost. Furthermore, static labels can only provide information for one variable, such as the players’ name. Imagine a general manager are presented these data and wishes to use them to make a trade. They may wish to procure a running back which has demonstrated his ability to catch the ball. There may be several data points of interest, but for each of them, the manager would need further information regarding each of the players such as his name, team, and perhaps age. We might not want to overwhelm the manager with all this information but allow them to easily retrieve it at will. In this case, we would like to make an interactive graph where information is embedded in the points.

To create such a graph, we can utilize the plotly package. We will create a ggplot with a sequence of labels for information we’d like to be accessed interactively. In this case, we will create the same faceted scatterplot as before with labels for the player’s team, name, and age. After we create the plot of interest, we can use the function ggplotly to create an interactive widget. This allows the user to pan the graph, zoom in on areas of interest, and hover over data points to retrieve more information about the observation. While this simply scratches the surface of plotly, it covers the most widely needed functionality. For more information regarding plotly, follow this link.

# create ggplot with labels
p <- ggplot(data = rush_receive, 
       aes(x = receiving_ctch_percent, 
           y = receiving_lng,
           color = pos,
           label = team,
           label2 = player,
           label3 = age)) +
  geom_point() +
  scale_color_brewer('Position', palette = 'Set1') +
  scale_x_continuous(name = 'Catch Percentage (%)') +
  scale_y_continuous(name = 'Longest Catch (yds)') +
  labs(title = 'Longest Catch vs Catch Percentage') +
  theme_bw() +
  theme(legend.position = 'none') +
  facet_wrap(~pos)

# create interactive plot, indicating which labels should be accessed interactively
# install.packages('plotly')
library(plotly)
ggplotly(p, tooltip = c('team', 'player', 'age'))