gganimate:让点前后停留几帧

gganimate: make points stay several frames before and after

我有一个 data.frame 包含不同类型的时间戳事件,地理定位。我知道如何使用 gganimate (*) 逐小时将每个事件的动画绘制为一个点。它会是这样的:

df = data.frame("id"  = runif(500, 1e6, 1e7),
           'lat' = runif(500, 45, 45.1),
           'long'= runif(500, 45, 45.1),
           'datetime'= seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=500),
           'hour'=format(seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=500), "%H"),
           'event'=paste0("type", rpois(500, 1)))

ggplot(data=df) + 
  aes(x=long, y=lat, color=factor(event)) + 
  geom_point() +
  transition_states(hour, state_length = 1, transition_length = 0)

现在我想在屏幕上使点 "stay" 更长,例如,如果某个事件发生在 5:00pm,我希望它在下午 2 点到晚上 8 点的动画中显示(3 帧在他的位置之前和之后,如果可能的话淡入淡出)。我不知道如何用 gganimate 做到这一点,我尝试使用 transition_length,但它提出了要点 "move",这对我来说毫无意义!

谢谢,

(*) 编辑:我想为每一行添加 6 个重复的行,并将小时从 -1 修改为 +3,但是它更重,不能' t 处理淡入淡出 in/out

library(magrittr)

df %<>% mutate(hour = hour + 2) %>% bind_rows(df)
df %<>% mutate(hour = hour + 1) %>% bind_rows(df)
df %<>% mutate(hour = hour - 4) %>% bind_rows(df)
df %<>% mutate(hour = hour %% 24 )

gganimate 似乎没有设置为处理图上的遗留点。我认为您将不得不走手动路线。

这里有一个(稍微笨拙的)复制行的方法,包括设置它们应该显示的时间和偏移量(用于 alpha 控制淡入淡出):

df_withRange <-
  df %>%
  mutate(hour = parse_number(hour)) %>%
  split(1:nrow(.)) %>%
  lapply(function(x){
    lapply(-3:3, function(this_time){
      x %>%
        mutate(frame_time = hour + this_time
               , offset = this_time
               , abs_offset = abs(this_time))
    }) %>%
      bind_rows()
  }) %>%
  bind_rows() %>%
  mutate(
    frame_time = ifelse(frame_time > 23, frame_time - 24, frame_time)
    , frame_time = ifelse(frame_time < 0, frame_time + 24, frame_time)
  )

然后,这段代码设置了情节:

ggplot(data=df_withRange
       , aes(x=long
             , y=lat
             , color=factor(event)
             , alpha = abs_offset
             )) +
  geom_point() +
  transition_states(frame_time) +
  labs(title = 'Hour: {closest_state}') +
  scale_alpha_continuous(range = c(1,0.2))

剧情:

还有很多清理工作要做(例如淡入淡出级别等),但这至少应该是一个开始

您可以使用transition_components并指定3小时作为每个点的进入/退出长度。

数据:

set.seed(123)
n <- 50 # 500 in question
df = data.frame(
  id       = runif(n, 1e6, 1e7),
  lat      = runif(n, 45, 45.1),
  long     = runif(n, 45, 45.1),
  datetime = seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=n),
  hour     = format(seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=n), "%H"),
  event    = paste0("type", rpois(n, 1)))

代码:

df %>%
  mutate(hour = as.numeric(as.character(hour))) %>%

  ggplot() +
  aes(x=long, y=lat, group = id, color=factor(event)) + 

  # I'm using geom_label to indicate the time associated with each
  # point & highlight the transition times before / afterwards.
  # replace with geom_point() as needed
  geom_label(aes(label = as.character(hour))) +
  # geom_point() +

  transition_components(hour, 
                        enter_length = 3, 
                        exit_length = 3) +
  enter_fade() +
  exit_fade() +
  ggtitle("Time: {round(frame_time)}")

此方法也适用于日期时间变量:

df %>%
  ggplot() +
  aes(x = long, y = lat, group = id, color = factor(event)) +
  geom_label(aes(label = format(datetime, "%H:%M"))) +
  transition_components(datetime,
                        enter_length = as_datetime(hm("3:0")),
                        exit_length = as_datetime(hm("3:0"))) +
  enter_fade() +
  exit_fade() +
  ggtitle("Time: {format(frame_time, '%H:%M')}")