transition_states: Transition between several distinct stages of the data

View source: R/transition-states.R

transition_statesR Documentation

Transition between several distinct stages of the data

Description

This transition splits your data into multiple states based on the levels in a given column, much like ggplot2::facet_wrap() splits up the data in multiple panels. It then tweens between the defined states and pauses at each state. Layers with data without the specified column will be kept constant during the animation (again, mimicking facet_wrap).

Usage

transition_states(states, transition_length = 1, state_length = 1, wrap = TRUE)

Arguments

states

The unquoted name of the column holding the state levels in the data.

transition_length

The relative length of the transition. Will be recycled to match the number of states in the data

state_length

The relative length of the pause at the states. Will be recycled to match the number of states in the data

wrap

Should the animation wrap-around? If TRUE the last state will be transitioned into the first.

Label variables

transition_states makes the following variables available for string literal interpretation, in addition to the general ones provided by animate():

  • transitioning is a boolean indicating whether the frame is part of the transitioning phase

  • previous_state The name of the last state the animation was at

  • closest_state The name of the state closest to this frame

  • next_state The name of the next state the animation will be part of

Object permanence

transition_states uses the group aesthetic of each layer to identify which rows in the input data correspond to the same graphic element and will therefore define which elements will turn into each other between states. The group aesthetic, if not set, will be calculated from the interaction of all discrete aesthetics in the layer (excluding label), so it is often better to set it explicitly when animating, to make sure your data is interpreted in the right way. If the group aesthetic is not set, and no discrete aesthetics exists then all rows will have the same group. If the group aesthetic is not unique in each state, then rows will be matched first by group and then by index. Unmatched rows will appear/disappear, potentially using an enter or exit function.

Computed Variables

It is possible to use variables calculated by the statistic to define the transition. Simply inclose the variable in stat() in the same way as when using computed variables in aesthetics.

See Also

Other transitions: transition_components(), transition_events(), transition_filter(), transition_layers(), transition_manual(), transition_null(), transition_reveal(), transition_time()

Examples

anim <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
  geom_point() +
  labs(title = "{closest_state}") +
  transition_states(Species, transition_length = 3, state_length = 1)

# Use a unique group to avoid matching graphic elements
iris$group <- seq_len(nrow(iris))
anim1 <- ggplot(iris, aes(Sepal.Width, Petal.Width, group = group)) +
  geom_point() +
  labs(title = "{closest_state}") +
  transition_states(Species, transition_length = 3, state_length = 1) +
  enter_fade() +
  exit_fade()

# Set `wrap = FALSE` to avoid transitioning the last state to the first
anim2 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
  geom_point() +
  labs(title = "{closest_state}") +
  transition_states(Species, transition_length = 3, state_length = 1,
                    wrap = FALSE)


dgrtwo/gganimate documentation built on March 3, 2024, 8:15 a.m.