Search code examples
rgganimate

Why does transition_layer in gganimate add an extra layer of NA? Can it be removed?


In generating a plot using transition_layer in gganimate, I noticed that an additional layer is added of NA.

As a minimum example. Say I have 10 unique observations (labeled "obs") of x,y pairs for each of 5 groups (labeled "id").

library(tidyverse)
library(gganimate)

dat1 <- data.frame(expand.grid(id = seq(1,5,1),obs=seq(1,10,1))) %>% 
  mutate(x = runif(50,10,30),
         y = runif(50,100,300))

I want to create a gganimate object that transitions over each of the 5 groups. Essentially, this would be the equivilant of creating a unique x,y scatter plot for each group and then combining those 5 plots into a GIF. I can do this with the transition_layer function in gganimate as:

p <- dat1 %>% 
  ggplot(aes(x,y))+
  geom_point(data=filter(dat1,id==1))+
  geom_point(data=filter(dat1,id==2))+
  geom_point(data=filter(dat1,id==3))+
  geom_point(data=filter(dat1,id==4))+
  geom_point(data=filter(dat1,id==5))

layername <- c("one","two","three","four","five")

anim <- p + transition_layers(
  layer_length = 5,
  transition_length = 1,
  keep_layers = FALSE,
  from_blank = FALSE,
  layer_order = NULL,
  layer_names = layername
)+ 
  ggtitle('Layers',
          subtitle = '{closest_layer} of 5')

anim

The animation works as expected, transitioning through each of layers as can be seen in the title (e.g "one of 5", "two of 5",...,"five of 5"). However, at the end of the animation i get a break in the sequence. The layers go ..."five of 5", "one of 5", "NA of 5", "one of 5".

I'm not sure why the extra transitions in the animation are occurring.

Particularly since there are no NAs in the layers. Any idea why this may be occurring and how it can be fixed? Or maybe a better way to generate a similar output?

Edit: the layer object does not contain a layer for NA

 anim$layers
[[1]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

[[2]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

[[3]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

[[4]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

[[5]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

Solution

  • The issue arrises when setting a transition_length. This can be seen using the frame_vars() command:

    frame_vars()
        frame nframes progress transitioning previous_layer closest_layer next_layer nlayers
    1       1     100     0.01          TRUE           five           one        one       5
    2       2     100     0.02          TRUE           five           one        one       5
    3       3     100     0.03          TRUE           five           one        one       5
    4       4     100     0.04          TRUE           five           one        one       5
    5       5     100     0.05         FALSE            one           one        one       5
    6       6     100     0.06         FALSE            one           one        one       5
    7       7     100     0.07         FALSE            one           one        one       5
    8       8     100     0.08         FALSE            one           one        one       5
    9       9     100     0.09         FALSE            one           one        one       5
    10     10     100     0.10         FALSE            one           one        one       5
    11     11     100     0.11         FALSE            one           one        one       5
    12     12     100     0.12         FALSE            one           one        one       5
    13     13     100     0.13         FALSE            one           two        one       5
    14     14     100     0.14         FALSE            one           two        one       5
    15     15     100     0.15         FALSE            one           two        one       5
    16     16     100     0.16         FALSE            one           two        one       5
    17     17     100     0.17         FALSE            one           two        one       5
    18     18     100     0.18         FALSE            one           two        one       5
    19     19     100     0.19         FALSE            one           two        one       5
    20     20     100     0.20         FALSE            one           two        one       5
    21     21     100     0.21          TRUE            one           two        two       5
    22     22     100     0.22          TRUE            one           two        two       5
    23     23     100     0.23          TRUE            one           two        two       5
    24     24     100     0.24          TRUE            one           two        two       5
    25     25     100     0.25         FALSE            two           two        two       5
    26     26     100     0.26         FALSE            two           two        two       5
    27     27     100     0.27         FALSE            two           two        two       5
    28     28     100     0.28         FALSE            two           two        two       5
    29     29     100     0.29         FALSE            two           two        two       5
    30     30     100     0.30         FALSE            two           two        two       5
    31     31     100     0.31         FALSE            two           two        two       5
    32     32     100     0.32         FALSE            two           two        two       5
    33     33     100     0.33         FALSE            two         three        two       5
    34     34     100     0.34         FALSE            two         three        two       5
    35     35     100     0.35         FALSE            two         three        two       5
    36     36     100     0.36         FALSE            two         three        two       5
    37     37     100     0.37         FALSE            two         three        two       5
    38     38     100     0.38         FALSE            two         three        two       5
    39     39     100     0.39         FALSE            two         three        two       5
    40     40     100     0.40          TRUE            two         three      three       5
    41     41     100     0.41          TRUE            two         three      three       5
    42     42     100     0.42          TRUE            two         three      three       5
    43     43     100     0.43          TRUE            two         three      three       5
    44     44     100     0.44         FALSE          three         three      three       5
    45     45     100     0.45         FALSE          three         three      three       5
    46     46     100     0.46         FALSE          three         three      three       5
    47     47     100     0.47         FALSE          three         three      three       5
    48     48     100     0.48         FALSE          three         three      three       5
    49     49     100     0.49         FALSE          three         three      three       5
    50     50     100     0.50         FALSE          three         three      three       5
    51     51     100     0.51         FALSE          three         three      three       5
    52     52     100     0.52         FALSE          three          four      three       5
    53     53     100     0.53         FALSE          three          four      three       5
    54     54     100     0.54         FALSE          three          four      three       5
    55     55     100     0.55         FALSE          three          four      three       5
    56     56     100     0.56         FALSE          three          four      three       5
    57     57     100     0.57         FALSE          three          four      three       5
    58     58     100     0.58         FALSE          three          four      three       5
    59     59     100     0.59          TRUE          three          four       four       5
    60     60     100     0.60          TRUE          three          four       four       5
    61     61     100     0.61          TRUE          three          four       four       5
    62     62     100     0.62          TRUE          three          four       four       5
    63     63     100     0.63         FALSE           four          four       four       5
    64     64     100     0.64         FALSE           four          four       four       5
    65     65     100     0.65         FALSE           four          four       four       5
    66     66     100     0.66         FALSE           four          four       four       5
    67     67     100     0.67         FALSE           four          four       four       5
    68     68     100     0.68         FALSE           four          four       four       5
    69     69     100     0.69         FALSE           four          four       four       5
    70     70     100     0.70         FALSE           four          four       four       5
    71     71     100     0.71         FALSE           four          five       four       5
    72     72     100     0.72         FALSE           four          five       four       5
    73     73     100     0.73         FALSE           four          five       four       5
    74     74     100     0.74         FALSE           four          five       four       5
    75     75     100     0.75         FALSE           four          five       four       5
    76     76     100     0.76         FALSE           four          five       four       5
    77     77     100     0.77         FALSE           four          five       four       5
    78     78     100     0.78          TRUE           four          five       five       5
    79     79     100     0.79          TRUE           four          five       five       5
    80     80     100     0.80          TRUE           four          five       five       5
    81     81     100     0.81          TRUE           four          five       five       5
    82     82     100     0.82         FALSE           five          five       five       5
    83     83     100     0.83         FALSE           five          five       five       5
    84     84     100     0.84         FALSE           five          five       five       5
    85     85     100     0.85         FALSE           five          five       five       5
    86     86     100     0.86         FALSE           five          five       five       5
    87     87     100     0.87         FALSE           five          five       five       5
    88     88     100     0.88         FALSE           five          five       five       5
    89     89     100     0.89         FALSE           five          five       five       5
    90     90     100     0.90         FALSE           five           one       five       5
    91     91     100     0.91         FALSE           five           one       five       5
    92     92     100     0.92         FALSE           five           one       five       5
    93     93     100     0.93         FALSE           five           one       five       5
    94     94     100     0.94         FALSE           five           one       five       5
    95     95     100     0.95         FALSE           five           one       five       5
    96     96     100     0.96         FALSE           five           one       five       5
    97     97     100     0.97            NA           <NA>           one       <NA>       5
    98     98     100     0.98            NA           <NA>          <NA>       <NA>       5
    99     99     100     0.99            NA           <NA>          <NA>       <NA>       5
    100   100     100     1.00            NA           <NA>          <NA>       <NA>       5
    

    The first layer recieves a transition of 4 frames followed by 16 frames for the layer. The remaining four layers recieve 4 frames for the transition and 15 frames for the layer, which sums to 96 out of 100 frames. The missing 4 frames recieve values of NA.

    It is not yet clear to me why gganimate is creating the frames in this manner. It seems like the missing 4 frames should naturally be assigned to the four layers which only recieve 15 frames per layer.

    Since the original desired output was essentially a gif going throough each layer, the transition_length can be set to zero. This results in no frames of NA being added to the animation:

    dat1 <- data.frame(expand.grid(id = seq(1,5,1),obs=seq(1,10,1))) %>% 
      mutate(x = runif(50,10,30),
             y = runif(50,100,300))
    
    p <- dat1 %>% 
      ggplot(aes(x,y))+
      geom_point(data=filter(dat1,id==1))+
      geom_point(data=filter(dat1,id==2))+
      geom_point(data=filter(dat1,id==3))+
      geom_point(data=filter(dat1,id==4))+
      geom_point(data=filter(dat1,id==5))
    
    layername <- c("one","two","three","four","five")
    
    anim <- p + transition_layers(
      layer_length = 5,
      transition_length = 0,
      keep_layers = FALSE,
      from_blank = TRUE,
      layer_order = NULL,
      layer_names = layername
    )+ 
      ggtitle('Layers',
              subtitle = '{next_layer} of 5')
    
    anim
    
    frame_vars()
        frame nframes progress transitioning previous_layer closest_layer next_layer nlayers
    1       1     100     0.01          TRUE           five           one        one       5
    2       2     100     0.02         FALSE            one           one        one       5
    3       3     100     0.03         FALSE            one           one        one       5
    4       4     100     0.04         FALSE            one           one        one       5
    5       5     100     0.05         FALSE            one           one        one       5
    6       6     100     0.06         FALSE            one           one        one       5
    7       7     100     0.07         FALSE            one           one        one       5
    8       8     100     0.08         FALSE            one           one        one       5
    9       9     100     0.09         FALSE            one           one        one       5
    10     10     100     0.10         FALSE            one           one        one       5
    11     11     100     0.11         FALSE            one           one        one       5
    12     12     100     0.12         FALSE            one           two        one       5
    13     13     100     0.13         FALSE            one           two        one       5
    14     14     100     0.14         FALSE            one           two        one       5
    15     15     100     0.15         FALSE            one           two        one       5
    16     16     100     0.16         FALSE            one           two        one       5
    17     17     100     0.17         FALSE            one           two        one       5
    18     18     100     0.18         FALSE            one           two        one       5
    19     19     100     0.19         FALSE            one           two        one       5
    20     20     100     0.20         FALSE            one           two        one       5
    21     21     100     0.21          TRUE            one           two        two       5
    22     22     100     0.22         FALSE            two           two        two       5
    23     23     100     0.23         FALSE            two           two        two       5
    24     24     100     0.24         FALSE            two           two        two       5
    25     25     100     0.25         FALSE            two           two        two       5
    26     26     100     0.26         FALSE            two           two        two       5
    27     27     100     0.27         FALSE            two           two        two       5
    28     28     100     0.28         FALSE            two           two        two       5
    29     29     100     0.29         FALSE            two           two        two       5
    30     30     100     0.30         FALSE            two           two        two       5
    31     31     100     0.31         FALSE            two           two        two       5
    32     32     100     0.32         FALSE            two         three        two       5
    33     33     100     0.33         FALSE            two         three        two       5
    34     34     100     0.34         FALSE            two         three        two       5
    35     35     100     0.35         FALSE            two         three        two       5
    36     36     100     0.36         FALSE            two         three        two       5
    37     37     100     0.37         FALSE            two         three        two       5
    38     38     100     0.38         FALSE            two         three        two       5
    39     39     100     0.39         FALSE            two         three        two       5
    40     40     100     0.40         FALSE            two         three        two       5
    41     41     100     0.41          TRUE            two         three      three       5
    42     42     100     0.42         FALSE          three         three      three       5
    43     43     100     0.43         FALSE          three         three      three       5
    44     44     100     0.44         FALSE          three         three      three       5
    45     45     100     0.45         FALSE          three         three      three       5
    46     46     100     0.46         FALSE          three         three      three       5
    47     47     100     0.47         FALSE          three         three      three       5
    48     48     100     0.48         FALSE          three         three      three       5
    49     49     100     0.49         FALSE          three         three      three       5
    50     50     100     0.50         FALSE          three         three      three       5
    51     51     100     0.51         FALSE          three         three      three       5
    52     52     100     0.52         FALSE          three          four      three       5
    53     53     100     0.53         FALSE          three          four      three       5
    54     54     100     0.54         FALSE          three          four      three       5
    55     55     100     0.55         FALSE          three          four      three       5
    56     56     100     0.56         FALSE          three          four      three       5
    57     57     100     0.57         FALSE          three          four      three       5
    58     58     100     0.58         FALSE          three          four      three       5
    59     59     100     0.59         FALSE          three          four      three       5
    60     60     100     0.60         FALSE          three          four      three       5
    61     61     100     0.61          TRUE          three          four       four       5
    62     62     100     0.62         FALSE           four          four       four       5
    63     63     100     0.63         FALSE           four          four       four       5
    64     64     100     0.64         FALSE           four          four       four       5
    65     65     100     0.65         FALSE           four          four       four       5
    66     66     100     0.66         FALSE           four          four       four       5
    67     67     100     0.67         FALSE           four          four       four       5
    68     68     100     0.68         FALSE           four          four       four       5
    69     69     100     0.69         FALSE           four          four       four       5
    70     70     100     0.70         FALSE           four          four       four       5
    71     71     100     0.71         FALSE           four          four       four       5
    72     72     100     0.72         FALSE           four          five       four       5
    73     73     100     0.73         FALSE           four          five       four       5
    74     74     100     0.74         FALSE           four          five       four       5
    75     75     100     0.75         FALSE           four          five       four       5
    76     76     100     0.76         FALSE           four          five       four       5
    77     77     100     0.77         FALSE           four          five       four       5
    78     78     100     0.78         FALSE           four          five       four       5
    79     79     100     0.79         FALSE           four          five       four       5
    80     80     100     0.80         FALSE           four          five       four       5
    81     81     100     0.81          TRUE           four          five       five       5
    82     82     100     0.82         FALSE           five          five       five       5
    83     83     100     0.83         FALSE           five          five       five       5
    84     84     100     0.84         FALSE           five          five       five       5
    85     85     100     0.85         FALSE           five          five       five       5
    86     86     100     0.86         FALSE           five          five       five       5
    87     87     100     0.87         FALSE           five          five       five       5
    88     88     100     0.88         FALSE           five          five       five       5
    89     89     100     0.89         FALSE           five          five       five       5
    90     90     100     0.90         FALSE           five          five       five       5
    91     91     100     0.91         FALSE           five          five       five       5
    92     92     100     0.92         FALSE           five           one       five       5
    93     93     100     0.93         FALSE           five           one       five       5
    94     94     100     0.94         FALSE           five           one       five       5
    95     95     100     0.95         FALSE           five           one       five       5
    96     96     100     0.96         FALSE           five           one       five       5
    97     97     100     0.97         FALSE           five           one       five       5
    98     98     100     0.98         FALSE           five           one       five       5
    99     99     100     0.99         FALSE           five           one       five       5
    100   100     100     1.00         FALSE           five           one       five       5