Learn R Programming

gganimate (version 1.0.8)

transition_layers: Build up a plot, layer by layer

Description

This transition gradually adds layers to the plot in the order they have been defined. By default prior layers are kept for the remainder of the animation, but they can also be set to be removed as the next layer enters.

Usage

transition_layers(
  layer_length = 1,
  transition_length = 1,
  keep_layers = TRUE,
  from_blank = TRUE,
  layer_order = NULL,
  layer_names = NULL
)

Arguments

layer_length

The proportional time to pause at each layer before a new one enters

transition_length

The proportional time to use for the entrance of a new layer

keep_layers

Either an integer indicating for how many following layers the layers should stay on screen or a logical. In the case of the later, TRUE will mean keep the layer for the remainder of the animation (equivalent to setting it to Inf) and FALSE will mean to transition the layer out as the next layer enters.

from_blank

Should the first layer transition in or be present on the onset of the animation

layer_order

An alternative order the layers should appear in (default to using the stacking order). All other arguments that references the layers index in some way refers to this order.

layer_names

A character vector of names for each layers, to be used when interpreting label literals

Label variables

transition_layers 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_layer The name of the last layer the animation was showing

  • closest_layer The name of the layer the animation is closest to showing

  • next_layer The name of the next layer the animation will show

  • nlayers The total number of layers

Object permanence

transition_layer does not link rows across data to the same graphic element, so elements will be defined uniquely by each row and the enter and exit of the layer it belongs to.

See Also

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

Examples

Run this code
# Default is to use layer order and keep layers for duration of animation
anim <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  geom_smooth(colour = 'grey', se = FALSE) +
  geom_smooth(aes(colour = factor(gear))) +
  transition_layers(layer_length = 1, transition_length = 2) +
  enter_fade() + enter_grow()

# Start with the first layer already present
anim1 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  geom_smooth(colour = 'grey', se = FALSE) +
  geom_smooth(aes(colour = factor(gear))) +
  transition_layers(layer_length = 1, transition_length = 2,
                    from_blank = FALSE) +
  enter_fade() + enter_grow()

# Change the order of the layers
anim2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  geom_smooth(colour = 'grey', se = FALSE) +
  geom_smooth(aes(colour = factor(gear))) +
  transition_layers(layer_length = 1, transition_length = 2,
                    from_blank = FALSE, layer_order = c(3, 1, 2)) +
  enter_fade() + enter_grow()

# Keep layer 1 for the whole animation, but remove the 2nd layer as the 3rd
# enters
anim3 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  geom_smooth(colour = 'grey', se = FALSE) +
  geom_smooth(aes(colour = factor(gear))) +
  transition_layers(layer_length = 1, transition_length = 2,
                    from_blank = FALSE, keep_layers = c(Inf, 0, 0)) +
  enter_fade() + enter_grow() +
  exit_fade() + exit_shrink()

Run the code above in your browser using DataLab