Changing axis order during frame animation in plotly

Viewed 25

I want the y-axis in the plot to order by pop every year when the frames move forward. So when Africa overtakes Americas the axis should change accordingly.

library(plotly)
library(tidyverse)
library(gapminder)


pop_df <- gapminder %>% 
  group_by(continent, year) %>% 
  summarise(pop = sum(pop, na.rm = TRUE), .groups = "drop") 

pop_df %>% 
  mutate(continent = reorder(continent, pop)) %>% 
  plot_ly(y=~continent, x=~pop, frame = ~year) %>% 
  add_bars(color = ~ continent, text = ~formatC(pop/1000000, format = "f", digits = 1), textposition = "outside", showlegend =FALSE)
0 Answers
Related