transition_layers: Build up a plot, layer by layer

Description Usage Arguments Label variables Object permanence See Also Examples

View source: R/transition-layers.R

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

1
2
3
4
5
6
7
8
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():

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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()

gganimate documentation built on Oct. 23, 2020, 8:04 p.m.