Plotly animation in R; frames are not in correct order, mix up in frames

The data:

dput(LifeExpCH$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 
111)
> dput(LifeExpCH$Die)
c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13, 
16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31, 
35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129, 
143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424, 
458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149, 
1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711, 
4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342, 
2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27, 
13, 5, 2, 1)

I want to create a plotly animation in R. The animation is not working as intended. There is a mix up in the frames. Frames 100:109 are at the start. May I ask for some help on how to get the frames in the right order?

Here is the code:

library(plotly)
library(dplyr) 
library(purrr)


LifeExpCH %>%
split(.$Age) %>% accumulate(~bind_rows(.x, .y)) %>%
             set_names(0:111) %>%
             bind_rows(.id = "frame") %>%
             plot_ly(x = ~Age, y = ~Die) %>%
             add_lines(frame = ~frame, showlegend = FALSE)

I think bind_rows .id = "frame" creates a character vector of frame, rather than a numeric, so ordering can be weird.

a<-c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
  18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
  34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
  50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 
  66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 
  82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 
  98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 
  111)
b<-c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13, 
  16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31, 
  35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129, 
  143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424, 
  458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149, 
  1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711, 
  4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342, 
  2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27, 
  13, 5, 2, 1)


df <- data.frame(age = a,
                 die = b)

library(plotly)
library(dplyr) 
library(purrr)
flist <- map(1:nrow(df),
             ~ mutate(head(df,.),frame=.))


df2 <- bind_rows(flist)

df2 %>%
  plot_ly(x = ~age, 
          y = ~die ,
          frame = ~frame,
          type = "scatter",
          mode='lines'
          ) %>% animation_opts(20) %>% 
  layout(showlegend=FALSE)

Hi Nirgrahamuk, thank you very much for your reply. It got also solved by adding mutate(frame = as.numeric(frame)) %>%before add_lines. Kind regards

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.