代码之家  ›  专栏  ›  技术社区  ›  Javi_VM

gganimate的等价物::transition_events on plotly

  •  0
  • Javi_VM  · 技术社区  · 4 年前

    在R中,使用gganimate,可以制作一个动画情节,其中事件随着时间的推移而出现和消失。例如:

    library(lubridate)
    library(gganimate)
    
    df=data.frame(
    x=c(1,2,3,4),
    y=c(1,2,3,4),
    start=c(1,2,3,4),
    end=c(5,6,7,8),
    en=as_date(1),
    ex=as_date(1))
    
    ggplot(data=df, aes(x=x,y=y))+
      geom_point()+
      gganimate::transition_events(
         start=start, 
         end=end, 
         enter_length = as.numeric(en), 
         exit_length = as.numeric(ex))
    
    

    这将生成一个图,其中点根据“开始”列显示,并根据“结束”列取消显示。

    我想知道是否有一种简单的方法可以通过plotly(最好使用 ggplotly() ),使滑块沿时间移动。

    0 回复  |  直到 4 年前
        1
  •  2
  •   ismirsehregal    4 年前

    下面是一个使用 ggplotly 然而,结果并不完全相同:

    library(plotly)
    library(lubridate)
    
    df = data.frame(
      x = c(1, 2, 3, 4),
      y = c(1, 2, 3, 4),
      start = c(1, 2, 3, 4),
      end = c(5, 6, 7, 8),
      en = as_date(1),
      ex = as_date(1)
    )
    
    frame_list <- Map(seq, from = df$start, to = df$end)
    DF <- data.frame(x = rep(df$x, times = lengths(frame_list)),
                     y = rep(df$y, times = lengths(frame_list)),
                     frame = unlist(frame_list))
    
    p <- ggplot(DF, aes(x, y)) +
      geom_point(aes(size = y, frame = frame))
    
    fig <- ggplotly(p)
    
    fig %>%
      animation_opts(
        frame = 0,
        easing = "linear",
        redraw = FALSE,
        mode = "immediate"
      )
    fig
    

    result