The Example

df <- data.frame(
  x = c(1,2,3), 
  y = c(1,2,3), 
  f = c(1,2,3)
)

fig <- df %>%
  plot_ly(
    x = ~x,
    y = ~y,
    frame = ~f,
    type = 'scatter',
    mode = 'markers',
    showlegend = F
  )

fig

Mulitple Trace Animations

df1 <- gapminder 
fig <- df1 %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  )
fig <- fig %>% layout( # To make the scale of Xasis is log
    xaxis = list(
      type = "log" 
    )
  )

fig

Add Animation Options

fig <- fig %>%
  animation_opts(
    frame = 1500, easing = "sin", redraw = FALSE #The higher value of frame make the animation becomes slower
  ) # Easing for the transition
 #Easing can be: linear, elastic, quad, quad-in, cubic, sin, exp...

fig

Add Button Options

fig <- fig %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )

fig

Add Slider Options

fig <- fig %>%
  animation_slider(
    currentvalue = list(prefix = "Year ", font = list(color="#00adef"))
  )

fig

Dash for R

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

#app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)