gganimate:数据仅出现在某些帧中

gganimate: data present only in some frames

我在动画绘图时遇到问题,其中某些图层中的数据仅出现在某些帧中。在下面的示例中,我有一个可以沿 9 帧很好地设置动画的移动点。但是,当我添加另一个图层,其中一个点仅出现在某些帧中时,出现以下错误:

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

示例

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

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

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

p = ggplot() +
      geom_point(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)

您可以附加数据表并调用它,如下所示:

  # 9 points along x=y; present at every time point
  dtP1 = data.table(x = 1:9,
                    y = 1:9,
                    t = 1:9,
                    dtp=rep("dtP1",9))
  
  # 3 points along x = 10-y; present at time points 2, 5, 8
  dtP2 = data.table(x = c(1, 5, 9),
                    y = c(9, 5, 1),
                    t = c(2, 5, 8), dtp=rep("dtP2",3))
  dtP <- rbind(dtP1,dtP2)
  
  p = ggplot() +
    geom_point(data = dtP,
               aes(x = x,
                   y = y,
               color = dtp), size=4) +
    
    gganimate::transition_time(t) +
    gganimate::ease_aes('linear')
  
  location <- "C:\My Disk Space\_My Work\RStuff\GWS\"
  
  anim_save("usegeom_point2.gif",p,location)