I’m trying to smoothen transitions in my race bar chart using gganimate
but still it looks a bit jerky to me. I’ve tried spline interpolation from this question: Animated sorted bar chart with bars overtaking each other but I’m not able to achieve the same result. I can’t update here the video since the file is too large.
Here is a sample of my initial data frame:
tt<- structure(list(Season = c("2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024"), FULLDATE = structure(c(19560,
19561, 19564, 19566, 19567, 19568, 19573, 19574, 19575, 19580,
19581, 19582), class = "Date"), `Super League` = c(7, 12, 15,
15, 22, 36, 36, 48, 58, 58, 67, 79), `Jupiler Pro League` = c(NA,
NA, NA, 2, 12, 21, 22, 22, 22, 22, 22, 22), Liga = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, 5, 11, 16), LigaNOS = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, 3, 16, 26), `Ligue 1` = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, 2, 5, 32), `Premier League` = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, 3, 21, 27)), row.names = c(NA, -12L
), class = c("tbl_df", "tbl", "data.frame"))
And here is my code:
df2<-tt %>%
gather(key=League,
value= goals, -FULLDATE, -Season)
df2<- df2 %>%
group_by(Season,FULLDATE) %>%
arrange(FULLDATE, -goals) %>%
mutate(rank=1:n()) %>%
filter(rank<=8) %>%
ungroup()
df2<-df2 %>%
group_by(League) %>%
complete(FULLDATE=full_seq(FULLDATE,1)) %>%
mutate(goals = approx(x=FULLDATE,y=goals, xout = FULLDATE,method="linear")$y) %>%
mutate(rank=approx(x=FULLDATE,y=rank,xout=FULLDATE)$y) %>%
ungroup() %>%
arrange(League,FULLDATE)
df2$goals[is.na(df2$goals)]<-0
df2$rank[is.na(df2$rank)]<-0
df2$image_file<- paste0("~/lglogos/",df2$League,".png")
colpal<-c('Ligue 1'="#E5174F",'Premier League' ="#99CBE7", 'Liga' = "#122623", 'Bundesliga' = "#00bbf9ff", 'Serie A' ="#3469A6",
"LigaNOS" = "#5EB1BF","Jupiler Pro League" ="#F6AE2D","Super League" = "#B3001B")
colpal.order<-c('Ligue 1','Premier League', 'Liga', 'Bundesliga', 'Serie A',"LigaNOS","Jupiler Pro League",
"Super League")
gfb<-ggplot(df2, aes(rank,group=League,fill=factor(League,levels = colpal.order),
color = as.factor(League)))+
geom_tile(aes(y=goals/2,x=rank,height=goals,width = 0.5),alpha = 0.8, color = NA) +
ggtext::geom_richtext(
aes(
y = 0,
x = rank,
label = sprintf("<img src='%s' width='70'/>", image_file)),
hjust = 1,
inherit.aes = FALSE,
fill = NA,
color = NA,
label.padding = grid::unit(30, "pt"))+
scale_fill_manual(values = colpal)+
scale_color_manual(values = colpal) +
coord_flip(clip="off",expand=FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse()+
geom_text(aes(y=goals, label= ifelse(goals > 0, sprintf("%1.0f", goals), "")),
color="white",fontface = "bold", size = 20,family="Archivo Black",vjust = 0.5, hjust = 1)+
guides(color = guide_none())+
theme_minimal()+
theme( panel.grid = element_blank(),
panel.grid.major.x = element_line( linewidth = .5, color="#122623"),
panel.background = element_rect(colour = NA,fill="transparent"),
legend.position = "none",
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(size=20,face = 'bold'),
plot.margin = margin(8, 6, 8, 6, "cm"),
plot.background = element_rect(fill = "#ffffff", color = NA),
plot.title = element_text(size = 28, hjust = 0.5, face = 'bold', color = "#21FA90", vjust = 5,family="Archivo Black"),
plot.subtitle = element_text(size = 17.5, hjust = 0.5, face = "italic", color = "grey", vjust = 5,family="Archivo Black"),
plot.caption = element_text(size = 12.5, color = "grey", vjust = 0,family="Archivo Black"),
)
anim <- gfb +
transition_states(FULLDATE, transition_length = 1,state_length = 1) +
view_follow(
fixed_y = c(0, NA),
fixed_x = TRUE,
)+
enter_grow() +
exit_shrink() +
ease_aes('linear') +
labs( title = "Goal scored this season",
subtitle = 'Month: {closest_state}',
caption = 'Source: Hedgeflare calculation')
anim
b<- animate(anim, fps = 60, duration =95, width = 1080, height = 1920, renderer = av_renderer())
anim_save("v1.mp4",b)
Thank you for your help.