在使用 gganimate 中的 transition_layer 生成一个图时,我注意到多了一层 NA。
举个最小的例子。假设我有10个唯一的观测值(标注为 “obs”),每组有5个x,y对(标注为 “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。我可以用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的1″、”5的NA”、”5的1″。
我不知道为什么在动画中会出现额外的转场。特别是图层中没有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穿过每一层,过渡长度可以设置为零。这将导致没有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