为每个“组”从起点到终点进行动画处理

时间:2019-01-19 20:20:46

标签: r ggplot2 gganimate

我有一个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_stateswrap = FALSE来使动画根据此SO solution进行“倒带”。但是,当我设置wrap = FALSE时,出现错误消息:

  

data.frame(...,check.names = FALSE)中的错误:参数隐含   不同的行数: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)

1 个答案:

答案 0 :(得分:0)

您可以调整以下代码以查看其是否满足您的需求。

出于演示目的,我只使用了前10个事件索引,为每个索引应用了不同的颜色,并放慢了动画的速度,以更好地说明这两种情况:

  1. 同一点从位置A移动到位置B(在同一事件索引内);
  2. 淡入/淡出(事件索引之间的转换)的不同点。

可以随意删除颜色映射并加快实际使用情况的动画速度。

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) 

this