带有因素的密谋动画

时间:2018-08-22 18:34:23

标签: r plotly r-plotly

将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 ...

按预期,关键帧按字母顺序排序: enter image description here


世代转换为建议的因子时,突然所有帧都画在彼此的顶部:

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 ... 

enter image description here

使用代码

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绘图进行动画处理?

已更新:根据请求genTotaldf的发送量

0 个答案:

没有答案