I am trying to do an animation to show the top 10 country in terms of GDP per captia. I am able to make the gif as below. I would like to make the label of country on the left side of y-axis. How can I do that?
Here is my code:
install.packages("ggplot2")
install.packages("data.table")
install.packages("gapminder")
library(gapminder)
library(ggplot2)
library(data.table)
gapminder10 = gapminder[,.SD[order(gdpPercap,decreasing = TRUE),][1:10],by = year]
gapminder10[, rank := 1:.N, by = year]
p <- ggplot(gapminder10) +
geom_bar(aes(x=gdpPercap, y=rank, fill = country), stat = "identity")+ # I need to use geom_bar to make the bar
geom_text(aes(x=0, y=rank, label = country), vjust = 0.2, hjust = "inward", size = 4) +
labs(x = "GDP per capita", y = "country")+
scale_y_reverse() + # reverse the y-axis
theme(legend.position="none", axis.text.y=element_blank(), axis.title.y=element_blank(),
axis.ticks=element_blank(),panel.background=element_blank())
g<-p + transition_time(year) +
labs(title = "Year: {frame_time}")
animate(g, nframes = 350,fps = 20, renderer = gifski_renderer())