How can I make a plotly animation load on the final frame. That is, when the plot loads, I would like it to load on that last frame (eg. 2017). By default, it loads on the first frame (eg. 1960). Later, when the person clicks play, the animation should start from the first frame (eg. 1960)
animation_opts(frame = frame, transition = 0, redraw = FALSE) %>%
animation_slider(currentvalue = list(prefix = "Year"))
By default, animations populate a play button and slider component for controlling the state of the animation (to pause an animation, click on a relevant location on the slider bar). Both the play button and slider component transition between frames according rules specified by animation_opts () . a plotly object.
Animations can be created by either using the frame argument in plot_ly () or the (unofficial) frame ggplot2 aesthetic in ggplotly (). By default, animations populate a play button and slider component for controlling the state of the animation (to pause an animation, click on a relevant location on the slider bar).
Now, along with data and layout, frames is added to the keys that figure allows. Your frames key points to a list of figures, each of which will be cycled through upon instantiation of the plot. To read more on animations see The Plotly Book.
Several Plotly Express functions support the creation of animated figures through the animation_frame and animation_group arguments. Here is an example of an animated scatter plot creating using Plotly Express. Note that you should always fix the x_range and y_range to ensure that your data remains visible throughout the animation.
This is not a complete answer, but since there is no other answer yet I decided to post the best that I can do, and maybe someone else has ideas to improve from there?
There is a function animation_slider()
, which takes the argument active
, which can set the active frame on the slider. However, this does not change the active frame in the plot... So the idea is to add a frame 0, (as also mentioned by Sixiang.Hu in the comments) and combine this with active frame. The downside with this solution is obviously that there is always a frame 0 when you move back on the animation slider.
# sample data
df = data.frame(a=seq(1,20),b=seq(1,20),frame=seq(1,20))
# add initial frame
init_frame = 10
df = rbind(df[df$frame==10,],df)
df$frame[1]=0
library(plotly)
plot_ly(df,x=~a,y=~b,frame=~frame) %>% animation_slider(active=init_frame)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With