Intro to Animations in R

How to create animations in R with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Frames

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.

Basic Example

library(plotly)

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

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

fig
Click to copy

Mulitple Trace Animations

library(plotly)
library(gapminder)

df <- gapminder 
fig <- df %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  )
fig <- fig %>% layout(
    xaxis = list(
      type = "log"
    )
  )

fig
Click to copy
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniayear: 1952195219571962196719721977198219871992199720022007gdpPercaplifeExpPlay

Add Animation Options

library(plotly)



fig <- fig %>%
  animation_opts(
    1000, easing = "elastic", redraw = FALSE
  )

fig
Click to copy
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniayear: 1952195219571962196719721977198219871992199720022007gdpPercaplifeExpPlay

Add Button Options

library(plotly)



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

fig
Click to copy
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniayear: 1952195219571962196719721977198219871992199720022007gdpPercaplifeExpPlay

Add Slider Options

library(plotly)


fig <- fig %>%
  animation_slider(
    currentvalue = list(prefix = "YEAR ", font = list(color="red"))
  )

fig
Click to copy
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniaYEAR 1952195219571962196719721977198219871992199720022007gdpPercaplifeExpPlay

Advanced Example

library(plotly)

df <- gapminder 
fig <- df %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  )
fig <- fig %>% layout(
    xaxis = list(
      type = "log"
    )
  )
fig <- fig %>% animation_opts(
    1000, easing = "elastic", redraw = FALSE
  )
fig <- fig %>% animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )
fig <- fig %>% animation_slider(
    currentvalue = list(prefix = "YEAR ", font = list(color="red"))
  )

fig
Click to copy
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniaYEAR 1952195219571962196719721977198219871992199720022007gdpPercaplifeExpPlay

Reference

To read more on animations see The Plotly Book.

What About Dash?

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)
Click to copy