I recently came across a very useful tutorial on animated bar plots from datascienceplus: (How to build Animated Bar Plots using R | DataScience+) I began using it for a project of my own connected to my own system of ranking baseball players. I used the code as a template almost exactly, and have been able to create the animated bar graphs, but I'm running into two minor problems on my end:
-
The bars aren't beginning at the start of the y-axis, they are oddly starting at a mid point between the Name's quantitave variable and 0.
-
Instead of the quantitative variables being displayed on the right of the bar like in the post, they are being displayed directly to the right of the y-axis in the space that should be occupied by the bars extending to 0.
Here's an image for reference:
. I can only post one image per post, but you can see the difference at the link.Here's the code for reference, in case I glossed over something. I'm pretty new to R Studio, and was somewhat amazed I was even able to make the animation. I tried messaging the author and didn't get a response. If anyone knew how to fix any of the above problems, I'd appreciate it!
staticplot = ggplot(imssformatted2, aes(rank, group = Name,
fill = as.factor(Name), color = as.factor(Name))) +
geom_tile(aes(y = TotalScore,
height = TotalScore,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(Name, " ")), size = 4, hjust = 1) +
geom_text(aes(y = value_rel, label = TotalScore, size = 25, hjust = 0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y= element_blank(),
legend.position = "none",
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
plot.title = element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle = element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption = element_text(size=12, hjust=0.5, face="italic", color="grey"),
plot.background = element_blank(),
plot.margin = margin(2, 2, 2, 4, "cm"))
anim = staticplot + transition_states(iRole, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'Top 10 Players by Role : {closest_state}',
subtitle = "by imSS",
caption = "First Five Years of imSS Data | 2015-2019")
animate(anim, 200, fps = 0.92, width = 1200, height = 1000,
renderer = gifski_renderer("gganim.gif"))