gganimate:仅在某些帧中存在数据

问题描述

我对动画绘图有一个问题,其中某些图层中的数据仅存在于某些帧中。在下面的示例中,我有一个移动点,可以沿9帧很好地进行动画处理。但是,当我添加仅在某些框架中存在点的另一层时,会出现以下错误

错误:时间数据在所有图层中必须是同一类

示例

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,9),y = c(9,1),t = c(2,8))

p = ggplot() +
      geom_point(data = dtP1,aes(x = x,y = y),color = "#000000") +
      geom_point(data = dtP2,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,9),y = c(9,1),t = c(2,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)
  

output

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...