我正在尝试制作一个交互式的plotly图(使用plotly作为R),其中每个帧都是一个直方图。在每个帧的顶部,我想写一些文本。下面的最小示例说明了我想要实现的目标。
library(dplyr)
library(plotly)
dat <- tibble(x = rnorm(10000,0,1), f = rep(1:10, each = 1000))
dat2 <- tibble(x = -3, y = 70, f = seq(1,10), txt = paste0("frame ", seq(1,10)))
plot_ly() %>%
add_trace(data = dat,
x = ~x,
frame = ~f,
type = "histogram",
showlegend = FALSE) %>%
add_trace(data = dat2,
x = ~x,
y = ~y,
frame = ~f,
type = "scatter",
mode = "text",
text = ~txt,
showlegend = FALSE) %>%
animation_opts(
transition = 0,
frame = 100,
) %>%
config(staticPlot = TRUE)
这里的问题是,当我在每个帧的顶部添加文本时,动画开始 Flink 。如果我删除第二个add_trace
, Flink 就不存在了。你知道为什么会发生这种情况以及如何解决它吗?
1条答案
按热度按时间s3fp2yjn1#
你应该使用
add_annotations
而不是add_trace
。你可以稍微修改@kat here编写的代码,以确保文本在图的每一帧中平滑显示。在这种情况下,你必须确保每一帧的迭代都接受你的注解 Dataframe 的输入。下面是一些可复制的代码:创建于2023-03-28带有reprex v2.0.2