我有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)
答案 0 :(得分:1)
我不确定是否有可能。这个问题Animated subplots in R with shared x axis (plotly package)从未得到答案。但是,也许有一种方法可以使用ggplot2
,ggplotly
和facet_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)