gganimate:仅在某些帧中存在数据 [英] gganimate: data present only in some frames

查看:45
本文介绍了gganimate:仅在某些帧中存在数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem animating plots where data in some layers in present only in some of the frames. In the example below, I have a moving point that can be nicely animated along 9 frames. However, when I add another layer with a point present only in some of the frames, I get the following error:

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

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)

推荐答案

您可以追加数据表并按如下所示进行调用:

You can append the data tables and call it as shown below:

  # 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)
  

这篇关于gganimate:仅在某些帧中存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆