geom_label_repel labels disappearing with gganimate animation

I want the labels associated with the 6 highest scoring teams to show up at the end of the animation. They appear in the plot, but not when I animate. Any help would be appreciated!


Here is all the code:

#data 

premier_league_data = data.frame()

for(i in 1:length(json_data$resources$datahub$type)){
  if(json_data$resources$datahub$type[i]=='derived/csv'){
    
    path_to_file = json_data$resources$path[i]
    data <- read.csv(url(path_to_file))
    data <- data %>%
      select(Div,Date,HomeTeam,AwayTeam,
             FTHG,FTAG,FTR,HTHG,HTAG,HTR,
             Referee,HS,AS,HST,AST,HF,AF,HC,
             AC,HY,AY,HR,AR)
    premier_league_data = rbind(premier_league_data, data)
  }
}

data17 <- premier_league_data[381:(380*2),]%>%
  mutate(Date = dmy(Date))

home <- data17 %>%
  select(Date, HomeTeam, FTHG) %>%
  rename(Team = HomeTeam,
         FTG = FTHG)

away <- data17 %>%
  select(Date, AwayTeam, FTAG) %>%
  rename(Team = AwayTeam,
         FTG = FTAG)

all_goals <- rbind(home, away) %>%
  arrange(Date) %>%
  group_by(Team) %>%
  mutate(cumgoals = cumsum(FTG),
         final_id = row_number()) %>%
  arrange(desc(final_id), desc(cumgoals))

top6teams_all <- all_goals %>%
  group_by(Team) %>%
  filter(cumgoals == max(cumgoals) & final_id == max(final_id)) %>%
  arrange(desc(cumgoals)) 

score_cutoff_all <- top6teams_all$cumgoals[6]
# here is  where the problem may be? I added this and then it stopped working 

all_goals$Team <- reorder(all_goals$Team, -all_goals$cumgoals)

teams = unique(as.character(all_goals$Team))

all_goals <- all_goals %>%
  ungroup() %>%
  mutate(Team=fct_relevel(Team, levels = teams)) 

unique(all_goals$Team)
# plot 
all_goals_plot <- all_goals %>%
  mutate(label = if_else(final_id == max(final_id) & cumgoals >= score_cutoff_all, paste0(as.character(Team),": ", cumgoals), "")) %>%
  ggplot(aes(x = Date, y = cumgoals, color = Team, group = Team)) +
  geom_line() + 
  labs(title = "All Goals Scored from 2017-2018 Season",
       x="Date",
       y = "Goals Scored") +
  theme_minimal() + 
  geom_label_repel(aes(label=as.character(label)),
                   show.legend = FALSE,
                   size=5) +
  guides(col = guide_legend(ncol=2)) +
  scale_x_date(date_labels = "%b '%y",
               date_breaks = "1 month")
# animation 
all_goals_plot2 <- all_goals_plot +  
  geom_point() +
  transition_reveal(Date)

animate(all_goals_plot2, end_pause = 50, height = 800, width = 1200)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.