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 <- txhousing %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area")) %>%
accumulate_by(~date)
如果按照上面函数的方式实现一个累积动画,行数会增加太多。
我用一千帧和一万行。由于数据量大,正在进行的工作受到干扰。
https://plot.ly/r/cumulative-animations/
除了示例之外,还有其他方法可以创建累积动画吗?
帮我!
最佳答案
我目前面临同样的问题。描述的方法here不适用于几千行数据。
我没有完全有效的解决方案 ,但我的想法是根据 slider 值调整 x 轴范围,而不是重新使用每一帧的数据(参见示例图 p_range_slider
)。不幸的是,这并没有为我们提供“播放”按钮。
我认为可以使用 animation_slider()
以类似的方式,但 steps
参数传递给 animation_slider()
未评估(参见示例图 p_animation_slider
)。这些步骤仍然与动画帧相关联(如 ?animation_slider
中所述)。
更新:此行为是有意设计的,请参阅 sources :
# don't let the user override steps
slider$steps <- steps
还 build 了
subplot
两者共享 x 轴均未成功。library(plotly)
DF <- data.frame(
n = 1:50,
x = seq(0, 12, length = 50),
y = runif(n = 50, min = 0, max = 10)
)
steps <- list()
for (i in seq_len(nrow(DF))) {
steps[[i]] <- list(
args = list("xaxis", list(range = c(0, i))),
label = i,
method = "relayout",
value = i
)
}
# Custom range slider -----------------------------------------------------
p_range_slider <- plot_ly(
DF,
x = ~ x,
y = ~ y,
type = "scatter",
mode = "markers"
) %>% layout(title = "Custom range slider",
xaxis = list(range = steps[[1]]$args[[2]]$range),
sliders = list(
list(
active = 0,
currentvalue = list(prefix = "X-max: "),
pad = list(t = 20),
steps = steps)))
p_range_slider
# Animation slider --------------------------------------------------------
p_animation_slider <- plot_ly(
DF,
x = ~ x,
y = ~ y,
type = "scatter",
mode = "markers",
frame = ~ n
) %>% layout(title = "Animation slider") %>% animation_slider(
active = 6,
currentvalue = list(prefix = "X-max: "),
pad = list(t = 20),
steps = steps # custom steps are ignored
)
p_animation_slider
# subplot(p_range_slider, p_animation_slider, nrows = 2, margin = 0.05, shareX = TRUE)
这种方法似乎可行
animation_slider()
需要允许它是 steps
执行自定义操作的参数(从定义的框架中解开)。任何其他解决此问题的想法都受到高度赞赏。也许可以重现this在R中使用过滤器(避免轴重新缩放)的python api方法? - Filter in R
这是一个关于如何在 R 中使用过滤器变换和自定义范围 slider 的示例,但是仍然没有动画(没有预先计算每一帧):
DF <- data.frame(x = rep(1:10, 2), y = runif(20), group = rep(LETTERS[1:2], each = 10))
steps <- list()
for (i in seq_along(unique(DF$x))) {
steps[[i]] <- list(
args = list('transforms[0].value', i),
label = i,
method = "restyle",
value = i
)
}
p_filter_slider <- plot_ly(
DF,
x = ~ x,
y = ~ y,
color = ~ group,
type = "scatter",
mode = "lines+markers",
transforms = list(
list(
type = 'filter',
target = 'x',
operation = '<=',
value = ~ x
)
)
) %>% layout(title = "Custom filter slider",
xaxis = list(range = c(0.5, length(unique(DF$x))+0.5)),
sliders = list(
list(
active = 0,
currentvalue = list(prefix = "X-max: "),
pad = list(t = 20),
steps = steps))
)
p_filter_slider
附加信息:
动画 slider documentation
JS slider attributes
相关GitHub issue
RStudio 社区 question
这是相同的question在 plotly 论坛上。
关于r - 有没有其他方法可以实现 plotly R 的累积动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49871541/