Почему transition_layer в gganimate добавляет дополнительный слой NA? Это можно удалить? - PullRequest
1 голос
/ 13 апреля 2020

При создании графика с использованием transition_layer в gganimate я заметил, что добавлен дополнительный слой NA.

Как минимальный пример. Скажем, у меня есть 10 уникальных наблюдений (помеченных "obs") пар x, y для каждой из 5 групп (помеченных "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))

Я хочу создать объект gganimate, который переходит по каждой из 5 групп. По сути, это было бы равнозначно созданию уникального графика рассеяния x, y для каждой группы, а затем объединению этих 5 графиков в GIF. Я могу сделать это с помощью функции transition_layer в gganimate следующим образом:

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

Анимация работает, как и ожидалось, переходя через каждый из слоев, как видно из заголовка (например, «один из 5», «два из 5 ", ...," пять из 5 "). Однако в конце анимации я получаю разрыв в последовательности. Слои go ... "пять из 5", "один из 5", "NA из 5", "один из 5".

Я не уверен, почему дополнительные переходы в анимации происходят Тем более что в слоях нет NA. Любая идея, почему это может происходить и как это можно исправить? Или, может быть, лучший способ создать аналогичный результат?

Редактировать: объект слоя не содержит слой для 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 

1 Ответ

0 голосов
/ 20 апреля 2020

Проблема возникает при настройке transition_length. Это можно увидеть с помощью команды frame_vars ():

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

Первый уровень получает переход из 4 кадров, за которыми следуют 16 кадров для слоя. Оставшиеся четыре слоя получают 4 кадра для перехода и 15 кадров для слоя, который суммирует до 96 из 100 кадров. Недостающие 4 кадра получают значения NA.

Мне пока не ясно, почему gganimate создает кадры таким образом. Кажется, что пропущенные 4 кадра естественно должны быть назначены четырем слоям, которые получают только 15 кадров на слой.

Поскольку исходный желаемый результат по существу представлял собой рисунок, проходящий через каждый слой, значение transition_length можно установить равным нулю. Это приводит к тому, что кадры NA не добавляются к анимации:

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
...