子情节中的多个动画

时间:2019-03-28 19:51:25

标签: r ggplot2 plotly

仅使用一个播放按钮尝试使用R中的Plotly动画化多个情节

为了获取我的每个数据条目的框架值,我使用:

    accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}



    d <- df3 %>% select(gen,score) %>% 
      accumulate_by(~gen)
    d2 <- df3 %>% select(gen,cpg_margin) %>% 
      accumulate_by(~gen)

接下来,我创建情节

p1 <- d %>%
  plot_ly(
    x = ~gen, 
    y = ~score,
    frame = ~frame, 
    type = 'scatter',
    mode = 'lines', 
    line = list(simplyfy = F)
  ) %>% 
  layout(
    xaxis = list(
      title = "title",
      zeroline = F
    ),
    yaxis = list(
      title = "title",
      zeroline = F
    ),
    title = "title"
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) 

p2 <- d %>%
  plot_ly(
    x = ~gen, 
    y = ~score2,
    frame = ~frame, 
    type = 'scatter',
    mode = 'lines', 
    line = list(simplyfy = F)
  ) %>% 
  layout(
    xaxis = list(
      title = "title",
      zeroline = F
    ),
    yaxis = list(
      title = "title",
      zeroline = F
    ),
    title = "title"
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) 

最后将两个图合并在一起的子图

p <- subplot(p1, p2) %>%
  animation_slider(
    hide = T
    ) %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )

但是播放按钮不会启动动画。我还需要在剧情中链接播放按钮吗?

0 个答案:

没有答案