Ung*_*rer 6 animation r 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 <- txhousing %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area")) %>%
accumulate_by(~date)
Run Code Online (Sandbox Code Playgroud)
如果以上述功能的方式实现累积动画,则行数将增加太多。
我用一千帧,一万行。由于大量数据,正在进行的工作受到了干扰。
https://plot.ly/r/cumulative-animations/
除示例外,还有什么方法可以创建累积动画?帮我!
我目前面临同样的问题。此处描述的方法不适用于几千行数据。
我没有完全可行的解决方案,但我的想法是根据滑块值调整 x 轴范围,而不是重新使用每个帧的数据(请参见示例图p_range_slider
)。不幸的是,这并没有为我们提供“播放”按钮。
我认为可以以animation_slider()
类似的方式使用,但steps
传递给的参数animation_slider()
不会被评估(参见示例图p_animation_slider
)。这些步骤仍然与动画帧相关(如 中所述?animation_slider
)。
更新:此行为是有意设计的,请参阅来源:
# don't let the user override steps
slider$steps <- steps
Run Code Online (Sandbox Code Playgroud)
此外,构建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)
Run Code Online (Sandbox Code Playgroud)
看来这种方法的工作animation_slider()
需要允许它的steps
参数执行自定义操作(与定义的框架无关)。任何其他解决此问题的想法都受到高度赞赏。
也许可以在 R 中使用过滤器(避免轴重新缩放)为 python api重现这种方法?- R 中的过滤器
下面是一个关于如何在 R 中使用过滤器变换和自定义范围滑块的示例,但是仍然没有动画(无需预先计算每一帧):
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
Run Code Online (Sandbox Code Playgroud)
附加信息:
动画滑块文档
JS滑块属性
相关 GitHub问题
RStudio 社区问题
这是plotly 论坛上同样的问题。