With a basketball dataset, I'm trying to build an animation of a line graph with a metric called EPV on y-axis and time on x-axis. I can get this to work, but now I'd like to add text labels at specific time points where any event
occurs. An event
is when an action happens in a basketball game, like a possession, dribble, or pass. Here is what I have so far:
library(tidyverse)
library(gganimate)
library(ggrepel)
theme_set(theme_minimal())
epv_curve_228 <- read_csv("https://raw.githubusercontent.com/jasonbaik94/stackoverflow-data/master/epv_curve_228.csv")
epv_curve_228 %>%
ggplot() +
geom_path(aes(x = 720 - game_clock, y = epv.smooth)) +
# Add description of event for home players
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = h1_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = h2_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = h3_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = h4_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = h5_event), force = 5) +
# Add description of event for away players
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = a1_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = a2_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = a3_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = a4_event), force = 5) +
geom_label_repel(aes(x = 720 - game_clock, y = epv.smooth, label = a5_event), force = 5) +
transition_reveal(720 - game_clock) +
labs(x = "Time (Seconds)",
y = "EPV",
title = "Possession #228 EPV")
The problem is that NOT ALL OF the text labels (Events such as Dribble, Possession, etc) appear. I know that the NA labels can be eliminated by replacing the NA with "". I'm not sure how I can show all the text labels associated with all the events. I see at least 4 different event
values