【发布时间】:2020-04-02 20:19:55
【问题描述】:
我正在尝试使用plotly 在 R 中创建一个动画,其中一个点可以根据当前时间的状态改变颜色。每个点都可以随时从一个阶段过渡到另一个阶段,我认为plotly 由于颜色的变化而失去了对点过渡的跟踪。
在下面的代码中,您会看到动画很流畅,没有任何颜色。但是当颜色包含在情节中时,这些点在动画过程中开始出现异常行为——它们要么被卡住,要么消失,要么在整个情节中跳跃。
您有什么建议可以解决这个问题,或者可能是另一种方法来为 R 中的转换状态设置动画?
library(plotly)
plot_colors <- c(red = "red", gray = "gray")
df <-
data.frame(step = rep(1:10, 2),
id = c(rep(1, 10), rep(2, 10)),
x = c(1:10, 1:10),
y = c(1:10, 10:1),
col = sample(c("red", "gray"), 20, replace = TRUE))
# This animation is smooth
df %>% plot_ly(x = ~x, y = ~y, frame = ~step, mode = "markers", type = "scatter")
# This animation has the points jumping all over the screen, and points disappear
df %>% plot_ly(x = ~x, y = ~y, frame = ~step, color = ~col,
colors = plot_colors, mode = "markers", type = "scatter")
【问题讨论】: