每个 "group" 从起点到终点的动画
Animating from start point to end point for each "group"
我有一个 x, y 坐标数据集,df
,我用 gganimate
:
制作动画
structure(list(event_index = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L,
12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L,
19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L,
25L, 26L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L, 30L, 31L, 31L,
32L, 32L, 33L, 33L, 34L, 34L, 35L, 35L, 36L, 36L, 37L, 37L, 38L,
38L, 39L, 39L, 40L, 40L, 41L, 41L, 42L, 42L, 43L, 43L, 44L, 44L,
45L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L, 50L, 50L, 51L,
51L, 52L, 52L, 53L, 53L, 54L, 54L, 55L, 55L, 56L, 56L), coords_x = c(80,
82, 53, 82, 31, 82, -56, -82, -34, -82, -33, -82, -40, -82, 30,
82, -66, -82, -36, -82, 45, 82, 17, 82, -6, 82, 47, 82, -51,
-82, -31, -82, -69, -82, -86, -82, -70, -82, 80, 82, 65, 82,
-76, -82, -71, -82, 81, 82, -57, -82, 80, 82, 75, 82, 77, 82,
-71, -82, -40, -82, -83, -82, 62, 82, 77, 82, 76, 82, -61, -82,
69, 82, -45, -82, 68, 82, 31, 82, 58, 82, 61, 82, 80, 82, 34,
82, 80, 82, -85, -82, -37, -82, -57, -82, 76, 82, 14, 82, 49,
82, -82, -82, -34, -82, -36, -82, -83, -82, -84, -82, -55, -82
), coords_y = c(-1, 0, 14, 0, -30, 0, 17, 0, 26, 0, -23, 0, -37,
0, 17, 0, -32, 0, -18, 0, 25, 0, 17, 0, -38, 0, 21, 0, 28, 0,
22, 0, 17, 0, 13, 0, 10, 0, -37, 0, -17, 0, 9, 0, 18, 0, -11,
0, 21, 0, -7, 0, 3, 0, 3, 0, -38, 0, 31, 0, 8, 0, -30, 0, -2,
0, 4, 0, -5, 0, 15, 0, 10, 0, -30, 0, -34, 0, 20, 0, 27, 0, -4,
0, 8, 0, -18, 0, 19, 0, 32, 0, -21, 0, 0, 0, 40, 0, -4, 0, -30,
0, -24, 0, -28, 0, -2, 0, -3, 0, 34, 0), event_rinkside = c("R",
"R", "R", "R", "R", "R", "L", "L", "L", "L", "L", "L", "L", "L",
"R", "R", "L", "L", "L", "L", "R", "R", "N", "N", "N", "N", "R",
"R", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "R", "R",
"R", "R", "L", "L", "L", "L", "R", "R", "L", "L", "R", "R", "R",
"R", "R", "R", "L", "L", "L", "L", "L", "L", "R", "R", "R", "R",
"R", "R", "L", "L", "R", "R", "L", "L", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "L", "L", "L", "L",
"L", "L", "R", "R", "N", "N", "R", "R", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -112L))
我正在尝试将点从每个 event_index
的第一行移动到 event_index
的第二行。然后,我希望该点淡出并出现在下一个 event_index
.
的下一组坐标处
例如,对于 event_index = 1
,我希望点从 (80, -1) 移动到 (82, 0)。对于 event_index = 2
,从 (53, 14) 到 (82, 0) 等
在代码方面,我一直在尝试transition_states
和wrap = FALSE
根据这个制作动画"rewind"。但是,当我设置 wrap = FALSE
时,我收到一条错误消息:
Error in data.frame(..., check.names = FALSE) : arguments imply
differing number of rows: 111, 100
这是我试过的方法
df %>%
ggplot(aes(coords_x, coords_y, group = event_index)) +
geom_point() +
coord_fixed() +
xlim(-100, 100) +
ylim(-45, 45) +
transition_states(states = event_index, wrap = FALSE)
您可以调整下面的代码,看看它是否适合您的需要。
出于演示的目的,我只使用了前 10 个事件索引,为每个索引应用了不同的颜色,并放慢了动画速度以更好地说明这两种情况:
- 同一点从位置A移动到位置B(在同一事件索引内);
- 不同的点淡出/淡入(事件索引之间的过渡)。
您可以随意删除颜色映射并加快实际用例的动画速度。
p <- df %>%
filter(event_index <= 10) %>%
mutate(state = seq(1, n())) %>%
ggplot(aes(x = coords_x, y = coords_y, group = event_index,
colour = factor(event_index))) +
geom_point(size = 5) +
scale_colour_brewer(palette = "Paired") +
transition_states(state, transition_length = 3, wrap = FALSE) +
# fade in for each point associated with a new event_index
enter_fade() +
# cubic for smoother transition between states within each event_index
ease_aes('cubic-in-out') +
# fade out for each point associated with an old event_index
exit_fade() +
# add title to keep track of states
labs(title = "State: {closest_state}")
# more frames at a slower fps (defaults are 100 / 10) to observe transitions better
animate(p, nframes = 200, fps = 5)
我有一个 x, y 坐标数据集,df
,我用 gganimate
:
structure(list(event_index = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L,
12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L,
19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L,
25L, 26L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L, 30L, 31L, 31L,
32L, 32L, 33L, 33L, 34L, 34L, 35L, 35L, 36L, 36L, 37L, 37L, 38L,
38L, 39L, 39L, 40L, 40L, 41L, 41L, 42L, 42L, 43L, 43L, 44L, 44L,
45L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L, 50L, 50L, 51L,
51L, 52L, 52L, 53L, 53L, 54L, 54L, 55L, 55L, 56L, 56L), coords_x = c(80,
82, 53, 82, 31, 82, -56, -82, -34, -82, -33, -82, -40, -82, 30,
82, -66, -82, -36, -82, 45, 82, 17, 82, -6, 82, 47, 82, -51,
-82, -31, -82, -69, -82, -86, -82, -70, -82, 80, 82, 65, 82,
-76, -82, -71, -82, 81, 82, -57, -82, 80, 82, 75, 82, 77, 82,
-71, -82, -40, -82, -83, -82, 62, 82, 77, 82, 76, 82, -61, -82,
69, 82, -45, -82, 68, 82, 31, 82, 58, 82, 61, 82, 80, 82, 34,
82, 80, 82, -85, -82, -37, -82, -57, -82, 76, 82, 14, 82, 49,
82, -82, -82, -34, -82, -36, -82, -83, -82, -84, -82, -55, -82
), coords_y = c(-1, 0, 14, 0, -30, 0, 17, 0, 26, 0, -23, 0, -37,
0, 17, 0, -32, 0, -18, 0, 25, 0, 17, 0, -38, 0, 21, 0, 28, 0,
22, 0, 17, 0, 13, 0, 10, 0, -37, 0, -17, 0, 9, 0, 18, 0, -11,
0, 21, 0, -7, 0, 3, 0, 3, 0, -38, 0, 31, 0, 8, 0, -30, 0, -2,
0, 4, 0, -5, 0, 15, 0, 10, 0, -30, 0, -34, 0, 20, 0, 27, 0, -4,
0, 8, 0, -18, 0, 19, 0, 32, 0, -21, 0, 0, 0, 40, 0, -4, 0, -30,
0, -24, 0, -28, 0, -2, 0, -3, 0, 34, 0), event_rinkside = c("R",
"R", "R", "R", "R", "R", "L", "L", "L", "L", "L", "L", "L", "L",
"R", "R", "L", "L", "L", "L", "R", "R", "N", "N", "N", "N", "R",
"R", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "R", "R",
"R", "R", "L", "L", "L", "L", "R", "R", "L", "L", "R", "R", "R",
"R", "R", "R", "L", "L", "L", "L", "L", "L", "R", "R", "R", "R",
"R", "R", "L", "L", "R", "R", "L", "L", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "L", "L", "L", "L",
"L", "L", "R", "R", "N", "N", "R", "R", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -112L))
我正在尝试将点从每个 event_index
的第一行移动到 event_index
的第二行。然后,我希望该点淡出并出现在下一个 event_index
.
例如,对于 event_index = 1
,我希望点从 (80, -1) 移动到 (82, 0)。对于 event_index = 2
,从 (53, 14) 到 (82, 0) 等
在代码方面,我一直在尝试transition_states
和wrap = FALSE
根据这个wrap = FALSE
时,我收到一条错误消息:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 111, 100
这是我试过的方法
df %>%
ggplot(aes(coords_x, coords_y, group = event_index)) +
geom_point() +
coord_fixed() +
xlim(-100, 100) +
ylim(-45, 45) +
transition_states(states = event_index, wrap = FALSE)
您可以调整下面的代码,看看它是否适合您的需要。
出于演示的目的,我只使用了前 10 个事件索引,为每个索引应用了不同的颜色,并放慢了动画速度以更好地说明这两种情况:
- 同一点从位置A移动到位置B(在同一事件索引内);
- 不同的点淡出/淡入(事件索引之间的过渡)。
您可以随意删除颜色映射并加快实际用例的动画速度。
p <- df %>%
filter(event_index <= 10) %>%
mutate(state = seq(1, n())) %>%
ggplot(aes(x = coords_x, y = coords_y, group = event_index,
colour = factor(event_index))) +
geom_point(size = 5) +
scale_colour_brewer(palette = "Paired") +
transition_states(state, transition_length = 3, wrap = FALSE) +
# fade in for each point associated with a new event_index
enter_fade() +
# cubic for smoother transition between states within each event_index
ease_aes('cubic-in-out') +
# fade out for each point associated with an old event_index
exit_fade() +
# add title to keep track of states
labs(title = "State: {closest_state}")
# more frames at a slower fps (defaults are 100 / 10) to observe transitions better
animate(p, nframes = 200, fps = 5)