I'm currently working with gganimate, and I can't find a way to properly order the bars of a barplot. I want to represent a mean by country, with an animation by year. I think I found how to do (see below), but I can't find how to order (and reorder) the country every year. I give an example below.
I tried with fct_reorder(), but I can't even get one year with the right order.
I'd like to see my bars switching order with the years.
d <- tibble(country = as.factor(sample(c("France", "Germany", "UK"), 100, replace = TRUE)),
year = sample(c(2000, 2002, 2004, 2006), 100, replace = TRUE),
revenu = rnorm(100, mean = 1500, sd= 400))
d %>% group_by(country, year) %>%
summarise(avg_revenu = mean(revenu, na.rm = TRUE)) %>%
ggplot(aes(fct_reorder(country, avg_revenu), avg_revenu)) +
geom_bar(stat = "identity") +
coord_flip() +
transition_states(year, transition_length = 1, state_length = 1) +
ggtitle("{closest_state}")
I found this page : How does gganimate order an ordered bar time-series? but I admit I don't fully understand the process, and my "mean" issue mess with my head...
Can anyone help me with this? Thanks!