1

我使用 制作了一个篮球动画gganimate,如下所示。您可以在我的博客文章中获得完整的动画(查看“Paul Pierce Isolation”下的动画)。

NBA动画

现在,这有一个问题:一旦秒数达到个位数,它只显示一个数字 (7)。它按照它的指示去做,但我希望它显示 (07) 以便它与 NBA 广播非常匹配。

NBA 转播示例

我正在考虑使用胶水包中的变压器,但我不确定我会怎么做。

这是我的代码:

# Function to grab jersey numbers
grab_jersey <- function(player_id) {

  swish_url <- paste0("https://www.swishanalytics.com/nba/players/player?id=", player_id)

  swish <- read_html(swish_url)

  result <- swish %>% 
    html_node(".mobile-hide") %>%
    html_text() %>% 
    # Extract out numeric
    parse_number()

  result
}



## Read in dataset
e.dat_test <- read_csv("https://raw.githubusercontent.com/howardbaek/nba-animation/master/test_df.csv")

# Replace _ent with jersey numbers
a1_ent_jersey <- e.dat_test %>% 
  pull(a1_ent) %>%
  first() %>% 
  grab_jersey()

a2_ent_jersey <- e.dat_test %>% 
  pull(a2_ent) %>%
  first() %>% 
  grab_jersey()

a3_ent_jersey <- e.dat_test %>% 
  pull(a3_ent) %>%
  first() %>% 
  grab_jersey()

a4_ent_jersey <- e.dat_test %>% 
  pull(a4_ent) %>%
  first() %>% 
  grab_jersey()

a5_ent_jersey <- e.dat_test %>% 
  pull(a5_ent) %>%
  first() %>% 
  grab_jersey()

h1_ent_jersey <- e.dat_test %>% 
  pull(h1_ent) %>%
  first() %>% 
  grab_jersey()

h2_ent_jersey <- e.dat_test %>% 
  pull(h2_ent) %>%
  first() %>% 
  grab_jersey()

h3_ent_jersey <- e.dat_test %>% 
  pull(h3_ent) %>%
  first() %>% 
  grab_jersey()

h4_ent_jersey <- e.dat_test %>% 
  pull(h4_ent) %>%
  first() %>% 
  grab_jersey()

h5_ent_jersey <- e.dat_test %>% 
  pull(h5_ent) %>%
  first() %>% 
  grab_jersey()

# Mutate jersey number columns
e.dat_test <- e.dat_test %>% 
  mutate(a1_ent_jersey = a1_ent_jersey,
         a2_ent_jersey = a2_ent_jersey,
         a3_ent_jersey = a3_ent_jersey,
         a4_ent_jersey = a4_ent_jersey,
         a5_ent_jersey = a5_ent_jersey,
         h1_ent_jersey = h1_ent_jersey,
         h2_ent_jersey = h2_ent_jersey,
         h3_ent_jersey = h3_ent_jersey,
         h4_ent_jersey = h4_ent_jersey,
         h5_ent_jersey = h5_ent_jersey) %>% 
  mutate(quarter_processed = case_when(
    quarter == 1 ~ "1ST",
    quarter == 2 ~ "2ND",
    quarter == 3 ~ "3RD",
    quarter == 4 ~ "4TH",
    TRUE ~ "NA"
  )) %>% 
  mutate(game_clock_minutes = game_clock %/% 60) %>% 
  mutate(game_clock_seconds = game_clock %% 60)


possid_quarter <- e.dat_test %>% 
  pull(quarter_processed) %>% 
  first()


# Save animation as object
anim <- fullcourt() +
  # Home Players + Jersey Numbers
  geom_point(data = e.dat_test, aes(x = h1_x, y = h1_y, group = possID), size = 6, color = "lightskyblue1") +  
  geom_text(data = e.dat_test, aes(x = h1_x, y = h1_y, group = possID, label = h1_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = h2_x, y = h2_y, group = possID), size = 6, color = "lightskyblue1") +  
  geom_text(data = e.dat_test, aes(x = h2_x, y = h2_y, group = possID, label = h2_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = h3_x, y = h3_y, group = possID), size = 6, color = "lightskyblue1") +  
  geom_text(data = e.dat_test, aes(x = h3_x, y = h3_y, group = possID, label = h3_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = h4_x, y = h4_y, group = possID), size = 6, color = "lightskyblue1") +  
  geom_text(data = e.dat_test, aes(x = h4_x, y = h4_y, group = possID, label = h4_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = h5_x, y = h5_y, group = possID), size = 6, color = "lightskyblue1") + 
  geom_text(data = e.dat_test, aes(x = h5_x, y = h5_y, group = possID, label = h5_ent_jersey), color = 'black', alpha = 0.3) + 

  # Away Players
  geom_point(data = e.dat_test, aes(x = a1_x, y = a1_y, group = possID), size = 6, color = "salmon1") +  
  geom_text(data = e.dat_test, aes(x = a1_x, y = a1_y, group = possID, label = a1_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = a2_x, y = a2_y, group = possID), size = 6, color = "salmon1") +  
  geom_text(data = e.dat_test, aes(x = a2_x, y = a2_y, group = possID, label = a2_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = a3_x, y = a3_y, group = possID), size = 6, color = "salmon1") +  
  geom_text(data = e.dat_test, aes(x = a3_x, y = a3_y, group = possID, label = a3_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = a4_x, y = a4_y, group = possID), size = 6, color = "salmon1") +  
  geom_text(data = e.dat_test, aes(x = a4_x, y = a4_y, group = possID, label = a4_ent_jersey), color = 'black', alpha = 0.3) + 

  geom_point(data = e.dat_test, aes(x = a5_x, y = a5_y, group = possID), size = 6, color = "salmon1") +  
  geom_text(data = e.dat_test, aes(x = a5_x, y = a5_y, group = possID, label = a5_ent_jersey), color = 'black', alpha = 0.3) + 

  # Ball
  geom_point(data = e.dat_test, aes(x = x, y = y, group = possID), size = 3, color = "gold") +

  transition_time(time = -game_clock) +
  ggtitle(paste0(possid_quarter, " ",  "{-frame_time %/% 60}", ":", "{round(-frame_time %% 60, 0)}")) +
  theme(plot.title = element_text(hjust = 0.5))


anim



4

0 回答 0