【问题标题】:gganimate: data present only in some framesgganimate:数据仅存在于某些帧中
【发布时间】:2020-09-24 09:30:48
【问题描述】:

我在绘制动画时遇到问题,其中某些图层中的数据仅存在于某些帧中。在下面的示例中,我有一个可以很好地沿 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, 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)

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

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

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

    【讨论】:

    • 太好了,谢谢!知道如果有两个具有不同几何形状的层会如何工作吗?事实上,在我最初的问题中,geom_point 存在于所有时间点,geom_tile 仅存在于其中一部分。
    • geom_point()+之后,可以添加geom_tile(data=subset(dtP, dtp=="dtP2"), aes(x=x, y=y)) +。然后你会得到一个从左上角到右下角的图块。
    • 就是这样,它不会产生预期的结果。磁贴出现在时间点 1-7 而不是 {2, 5, 8}。我已经创建了一个单独的question 来解决这个问题。
    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多