情节累积动画不适用于R中的子图

时间:2018-04-22 02:09:01

标签: r plotly

我有2个plotly图,其中累积动画可以单独使用。但如果我使用subplot将它们组合起来,它们就不起作用了。是否可以为2个组合图设置动画?以下是一个可重复的例子:

library(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)
}

library(MASS)
data(Traffic)


# data 1
Traffic1 <- Traffic %>%
  filter(year==1961) %>% 
  accumulate_by(~day)

# plot1
y_plot <- plot_ly(data = Traffic1,
                  x = ~day,
                  y = ~y,
                  frame = ~frame,
                  type = "scatter") %>%  
animation_slider(
  currentvalue = list(
    prefix = "Day"
  )
)


# data 2
Traffic2 <- Traffic %>%
  filter(year==1962) %>% 
  accumulate_by(~day)

# plot 2
y_plot2 <- plot_ly(data = Traffic2,
                  x = ~day,
                  y = ~y,
                  frame = ~frame,
                  type = "scatter") %>%  
  animation_slider(
    currentvalue = list(
      prefix = "Day"
    )
  )


# combined plot
subplot(y_plot, y_plot2)

1 个答案:

答案 0 :(得分:1)

我不确定是否有可能。这个问题Animated subplots in R with shared x axis (plotly package)从未得到答案。但是,也许有一种方法可以使用ggplot2ggplotlyfacet_grid

你可以试试这个:

#Create a new data frame
Traffic1$Traffic <- 1
Traffic2$Traffic <- 2
Traffic <- rbind(Traffic1, Traffic2)

gg <- ggplot(Traffic, aes(x=day, y=y)) +
    geom_point(aes(frame = frame), color = "blue") + facet_grid(Traffic~.) + theme_bw()
ggplotly(gg)

enter image description here