Search code examples
rggplot2gganimate

gganimate over time using transition_time where time variable is NBA game clock


I'm trying to animate the movement of players and basketball in a single NBA game. In the NBA, the game clock starts at 12 minutes and decreases to 11:59minutes, 11:58, 11:57minutes, etc. Therefore, the dataset of one possession has a game_clock variable that starts at 718.80 seconds (11.98 minutes). Here is what I've done so far:

gganimate code:

first_poss_so <- read_csv("https://raw.githubusercontent.com/jasonbaik94/stackoverflow-data/master/first_poss_so.csv")


fullcourt() +
  geom_point(data = first_poss_so, aes(x = h1_x, y = h1_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h2_x, y = h2_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h3_x, y = h3_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h4_x, y = h4_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h5_x, y = h5_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = a1_x, y = a1_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a2_x, y = a2_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a3_x, y = a3_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a4_x, y = a4_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a5_x, y = a5_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = x, y = y, group = possID), size=3, color = "gold") +
  transition_time(time = game_clock)

Here is full_court()

library(ggplot2)

fullcourt <- function () {

  palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
            "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

  #Generate Data for the 3 point line
  # Define the circle; add a point at the center if the 'pie slice' if the shape is to be filled
  circleFun <- function(center=c(0,5.25), diameter=20.9, npoints=20000, start=0, end=1, filled=TRUE){
    tt <- seq(start*pi, end*pi, length.out=npoints)
    df <- data.frame(
      y = center[1] + diameter / 2 * cos(tt),
      x = center[2] + diameter / 2 * sin(tt)
    )
    return(df)
  }

  halfCircle <- circleFun(c(0, 5.25), 20.9*2, start=0, end=1, filled=FALSE) 
  ggplot(data=data.frame(y=1,x=1),aes(x,y))+
    ###halfcourt line:
    geom_path(data=data.frame(x=c(47,47),y=c(0,50)))+
    ###outside boy:
    geom_path(data=data.frame(y=c(0,0,50,50,0),x=c(0,94,94,0,0)))+
    ###solid FT semicircle above FT line:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(19+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(75+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    ###dashed FT semicircle below FT line:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(19-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x),linetype='dashed')+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(75-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x),linetype='dashed')+
    ###kex:
    geom_path(data=data.frame(y=c(17,17,33,33,17),x=c(0,19,19,0,0)))+
    geom_path(data=data.frame(y=c(17,17,33,33,17),x=c(94,75,75,94,94)))+
    ###boy inside the kex:
    geom_path(data=data.frame(y=c(19,19,31,31,19),x=c(0,19,19,0,0)))+
    geom_path(data=data.frame(y=c(19,19,31,31,19),x=c(94,75,75,94,94)))+
    ###restricted area semicircle:
    geom_path(data=data.frame(y=c((-4000:(-1)/1000)+25,(1:4000/1000)+25),x=c(5.25+sqrt(4^2-c(-4000:(-1)/1000,1:4000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-4000:(-1)/1000)+25,(1:4000/1000)+25),x=c(88.75-sqrt(4^2-c(-4000:(-1)/1000,1:4000/1000)^2))),aes(y=y,x=x))+
    ###halfcourt semicircle:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(47-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(47+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    ###rim:
    geom_path(data=data.frame(y=c((-750:(-1)/1000)+25,(1:750/1000)+25,(750:1/1000)+25,(-1:-750/1000)+25),x=c(c(5.25+sqrt(0.75^2-c(-750:(-1)/1000,1:750/1000)^2)),c(5.25-sqrt(0.75^2-c(750:1/1000,-1:-750/1000)^2)))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-750:(-1)/1000)+25,(1:750/1000)+25,(750:1/1000)+25,(-1:-750/1000)+25),x=c(c(88.75+sqrt(0.75^2-c(-750:(-1)/1000,1:750/1000)^2)),c(88.75-sqrt(0.75^2-c(750:1/1000,-1:-750/1000)^2)))),aes(y=y,x=x))+
    ###backboard:
    geom_path(data=data.frame(y=c(22,28),x=c(4,4)),lineend='butt')+
    geom_path(data=data.frame(y=c(22,28),x=c(90,90)),lineend='butt')+
    ###three-point line:
    # geom_path(data=data.frame(y=c(-21,-21,-21000:(-1)/1000,1:21000/1000,21,21),x=c(0,169/12,5.25+sqrt(23.75^2-c(-21000:(-1)/1000,1:21000/1000)^2),169/12,0)),aes(y=y,x=x))+
    ###fiy aspect ratio to 1:1
    geom_path(data=halfCircle,aes(x=x,y=y+25))+
    ###Complete the three-point line 
    geom_path(data=data.frame(y=c(4.1,4.1,45.9,45.9),x=c(5.25,0,0,5.25)))+
    geom_path(data=halfCircle,aes(x=94-x,y=y+25))+
    geom_path(data=data.frame(y=c(4.1,4.1,45.9,45.9),x=c(88.75,94,94,88.75)))+
    coord_fixed()+

    ###Clean up the Court 
    theme_bw()+theme(panel.grid=element_blank(), legend.title=element_blank(), panel.border=element_blank(),axis.text=element_blank(),axis.ticks=element_blank(),axis.title=element_blank(),legend.position="top")}

The problem with my gganimate code is that transition_time renders the play backwards... because the game_clock starts from approx 12 minutes and decreases. Of course, I can subtract 12 minutes from game_clock, but that'd prevent me from using game_clock as a sensible label: ggtitle("Game Clock: {frame_time}")

enter image description here

Basically, how can I get transition_time to render time in decreasing order? (Start from 12 minutes and end at 0 minutes)


Solution

  • (As a practice I don't download external linked data that I don't know about, so I'm going to answer with an approach using standard data.)

    This should be possible to do by reversing the sign on your transition_time variable, and reversing the sign again in your title, like this:

    library(gapminder)
    library(gganimate)
    library(dplyr)
    
    a <- gapminder %>%
      ggplot(aes(gdpPercap, lifeExp, color = continent, group = country)) +
      geom_point() +
      transition_time(-year) +
      labs(title = "Year: {-frame_time}")
    
    animate(a, nframes = 50, duration = 5)
    

    enter image description here