r - 为什么gganimate中的transition_layer要多加一层NA?可以去掉吗?
问题描述
在 gganimate 中使用 transition_layer 生成绘图时,我注意到添加了一个额外的 NA 层。
作为一个最低限度的例子。假设我对 5 个组(标记为“id”)中的每一个组有 10 个独特的观察(标记为“obs”)的 x,y 对。
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))
我想创建一个在 5 个组中的每一个上转换的 gganimate 对象。本质上,这相当于为每个组创建一个独特的 x,y 散点图,然后将这 5 个图组合成一个 GIF。我可以使用 gganimate 中的 transition_layer 函数来做到这一点:
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 之五”)。但是,在动画结束时,我在序列中得到了休息。这些层去......“5个中的五个”,“5个中的一个”,“5个中的NA”,“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
解决方案
设置 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 帧用于图层,总计 100 帧中的 96 帧。丢失的 4 帧接收到 NA 值。
我还不清楚为什么 gganimate 会以这种方式创建框架。似乎丢失的 4 帧应该自然地分配给每层仅接收 15 帧的四个层。
由于原始所需的输出本质上是通过每一层的 gif,因此可以将 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
推荐阅读
- android-mediacodec - 带有 MediaProjection 的 Android MediaCodec
- ssl - 如何检查 kubernetes 令牌和证书密钥的到期时间?
- spring-boot - Spring Data JDBC UnsatisfiedDependencyException
- ios - 如何在swift中用变音符号分割字符串,如“ä”
- python - 如何计算神经网络中预测变量的相对重要性
- json - 尝试映射 JSON 字典 child_items 数组(如果存在)
- python - 无法使用 * with 导入
- flutter - 容器的顶部是彩色的,但其余部分是白色的。如何在颤动中做到这一点?
- c++ - c ++如何从文本文件中的特定行读取和写入?
- android - 如何使用 RxJava 2 Android 对来自传感器的微秒内实时数据进行计数、比较和存储在数组中