gganimate:具有不同几何形状和时间点的两层

gganimate: two layers with different geometries and timepoints

问题类似于 问题,但这里的两层使用不同的几何形状,geom_tilegeom_point。这个想法是让瓦片仅在第 2、5、8 帧出现在不同位置,并且点在每一帧中沿着对角线移动。

尝试运行以下示例时,出现错误:

Error: time data must be the same class in all layers

例子

require(data.table)
require(ggplot2)
require(gganimate)

# 3 tiles along x = 10-y; present at time points 2, 5, 8
dtP1 = data.table(x = c(1, 5, 9),
                  y = c(9, 5, 1),
                  t = c(2, 5, 8))

# 9 points along x=y; present at every time point
dtP2 = data.table(x = 1:9,
                  y = 1:9,
                  t = 1:9)

p = ggplot() +
    geom_tile(data = dtP1,
              aes(x = x,
                  y = y),
                  color = "#000000") +
    geom_point(data = dtP2,
               aes(x = x,
                   y = y),
               color = "#FF0000") +
    gganimate::transition_time(t) +
    gganimate::ease_aes('linear')

pAnim = gganimate::animate(p, 
                           renderer = av_renderer("~/test.mp4"), 
                           fps = 1, 
                           nframes = 9,
                           height = 400, width = 400)

以下对你有用吗?

library(dplyr)

p <- rbind(dtP1 %>% mutate(group = "group1"),
           dtP2 %>% mutate(group = "group2")) %>%
  tidyr::complete(t, group) %>%
  ggplot(aes(x = x, y = y)) +
  geom_tile(data = . %>% filter(group == "group1"),
            color = "black") +
  geom_point(data = . %>% filter(group == "group2"),
             color = "red") +
  ggtitle("{frame_time}") + # added this to show the frame explicitly; optional
  transition_time(t) +
  ease_aes('linear')

animate(p, nframes = 9, fps = 1)