gganimate animate multiple paths based on time

前提是你 提交于 2019-12-01 00:01:30

First of all, this is awesome.

First approach using shadow_wake and no other data prep

I commented out the pieces that weren't defined in the question. I added wrap = F to make the animation reset (rather than rewind) at the end, and shadow_wake to capture the trajectory.

# The factors of pos_type are backwards (b/c alphabetical), so R thinks the detonation 
#   comes before the player position. Here we reverse that.
df$pos_type <- forcats::fct_rev(df$pos_type)

ggplot(df, aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  transition_states(states=pos_type, transition_length = 1, state_length = 1, wrap = F) +
  shadow_wake(wake_length = 1)

Second approach, adding initial position and geom_segment

We could also add the trajectory as a segment, if we give each frame a reference to the player position:

df %>%
  # Add reference to first coordinates for each plot_group
  left_join(by = "plot_group",
    df %>% 
      group_by(plot_group) %>%
      filter(pos_type == "Player position") %>%
      mutate(pos_x1 = pos_x, pos_y1 = pos_y) %>%
      select(plot_group, pos_x1, pos_y1)
  ) %>%
ggplot(aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  geom_segment(color = "gray70", aes(xend = pos_x1, yend  = pos_y1)) +
  transition_states(states=pos_type, transition_length = 1, state_length = 1, wrap = F)

Third variation, showing trajectories by start time

Similar to 2nd, but I add each trajectory's distance, travel time, and start time. I assume here that the detonation is the end time, and work back to when the trajectory started.

(I first tried transition_time, but couldn't get it to work without buggy behavior after the first trajectory.)

# trajectory speed
dist_per_time = 50

df2 <- df %>%
  # Add reference to first coordinates for each plot_group
  left_join(by = "plot_group",
            df %>% 
              group_by(plot_group) %>%
              filter(pos_type == "Player position") %>%
              mutate(pos_x1 = pos_x, pos_y1 = pos_y) %>%
              select(plot_group, pos_x1, pos_y1)
  ) %>%
  left_join(by = c("plot_group", "pos_type"),
    df %>%
      group_by(plot_group) %>%
      mutate(x_d = (range(pos_x)[1] - range(pos_x)[2]),
             y_d = (range(pos_y)[1] - range(pos_y)[2]),
             dist = sqrt(x_d^2 + y_d^2),
             event_time = round_throw_time - if_else(pos_type == "Player position", 
                                                     dist / dist_per_time, 
                                                     0),
             event_time = round(event_time, 1)) %>%
      select(plot_group, pos_type, dist, event_time)
  )


  ggplot(df2, aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  geom_segment(color = "gray70", aes(xend = pos_x1, yend  = pos_y1)) +
  transition_reveal(plot_group, event_time)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!