将num矢量转换为因子会在plot_ly动画中产生意外结果。
如果frame是数字变量(或字符串),则框架始终按升序(字母顺序)排序;但对于因素,顺序反映的是plotly book
级的顺序
数据已设置:
>str(genTotal)
data.frame': 1620 obs. of 4 variables:
$ generation: int 0 0 0 0 0 0 0 0 0 0 ...
$ fitness : num 2.38 2.38 2.38 2.38 21.82 ...
$ x : num 0.946 0.946 0.946 0.946 -2.26 ...
$ x2 : num -0.0686 -0.0686 -0.0686 -0.0686 2.0935 ...
将世代转换为建议的因子时,突然所有帧都画在彼此的顶部:
genTotal$generation <- factor(genTotal$generation, ordered = TRUE)
'data.frame': 1620 obs. of 4 variables:
$ generation: Factor w/ 81 levels "0","50","100",..: 1 1 1 1 1 1 1 1 1 1 ...
$ fitness : num 2.38 2.38 2.38 2.38 21.82 ...
$ x : num 0.946 0.946 0.946 0.946 -2.26 ...
$ x2 : num -0.0686 -0.0686 -0.0686 -0.0686 2.0935 ...
使用代码
plot_ly(data = df, x = ~ x, y = ~y, z = ~rastrigin, type = "contour") %>%
colorbar(title = "Value") %>%
add_trace(data = genTotal,x=~x,y=~x2,type = "scatter", mode = "markers",
frame = genTotal$generation,
inherit = FALSE,
marker = list(size = 10,
color = 'rgba(255, 182, 193, .7)',
line = list(color = 'rgba(152, 0, 0, 1)',width = 2))
)
问题: 为什么突然将所有框架都画在彼此的顶部?
如何使用数字框架矢量正确地(有序地)对plot_ly绘图进行动画处理?