Learn R Programming

plotly (version 4.10.3)

animation_opts: Animation configuration options

Description

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). Both the play button and slider component transition between frames according rules specified by animation_opts().

Usage

animation_opts(
  p,
  frame = 500,
  transition = frame,
  easing = "linear",
  redraw = TRUE,
  mode = "immediate"
)

animation_slider(p, hide = FALSE, ...)

animation_button(p, ..., label)

Arguments

p

a plotly object.

frame

The amount of time between frames (in milliseconds). Note that this amount should include the transition.

transition

The duration of the smooth transition between frames (in milliseconds).

easing

The type of transition easing. See the list of options here https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js

redraw

Trigger a redraw of the plot at completion of the transition? A redraw may significantly impact performance, but may be necessary to update graphical elements that can't be transitioned.

mode

Describes how a new animate call interacts with currently-running animations. If immediate, current animations are interrupted and the new animation is started. If next, the current frame is allowed to complete, after which the new animation is started. If afterall all existing frames are animated to completion before the new animation is started.

hide

remove the animation slider?

...

for animation_slider, attributes are passed to a special layout.sliders object tied to the animation frames. The definition of these attributes may be found here https://github.com/plotly/plotly.js/blob/master/src/components/sliders/attributes.js For animation_button, arguments are passed to a special layout.updatemenus button object tied to the animation https://github.com/plotly/plotly.js/blob/master/src/components/updatemenus/attributes.js

label

a character string used for the animation button's label

Author

Carson Sievert

Examples

Run this code

df <- data.frame(
  x = c(1, 2, 2, 1, 1, 2),
  y = c(1, 2, 2, 1, 1, 2),
  z = c(1, 1, 2, 2, 3, 3)
)
plot_ly(df) %>%
  add_markers(x = 1.5, y = 1.5) %>%
  add_markers(x = ~x, y = ~y, frame = ~z)

# it's a good idea to remove smooth transitions when there is
# no relationship between objects in each view
plot_ly(mtcars, x = ~wt, y = ~mpg, frame = ~cyl) %>%
  animation_opts(transition = 0)

# works the same way with ggplotly
if (interactive()) {
  p <- ggplot(txhousing, aes(month, median)) +
    geom_line(aes(group = year), alpha = 0.3) +
    geom_smooth() +
    geom_line(aes(frame = year, ids = month), color = "red") +
    facet_wrap(~ city)
 
  ggplotly(p, width = 1200, height = 900) %>%
    animation_opts(1000)
}

  
#' # for more, see https://plotly.com/r/animating-views.html

Run the code above in your browser using DataLab