Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Plotly animation - initial frame

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"))
like image 432
Camila Vargas Restrepo Avatar asked Jul 13 '17 16:07

Camila Vargas Restrepo


People also ask

How to pause an animation in Plotly?

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.

How do I create an animation in ggplotly?

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).

How do Frames work in Plotly?

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.

How do I create an animated scatter plot using Plotly Express?

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.


1 Answers

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)
like image 58
Florian Avatar answered Nov 03 '22 08:11

Florian