【问题标题】:Colored Points Disappearing When Creating an Animation in R using Plotly使用 Plotly 在 R 中创建动画时彩色点消失
【发布时间】: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")

【问题讨论】:

    标签: r animation plotly


    【解决方案1】:

    尝试如下传递颜色参数marker = list(color = ~col)

    df %>% plot_ly(x = ~x, y = ~y, frame = ~step,
                   marker = list(color = ~col),
                     mode = "markers", type = "scatter")
    

    增加标记大小并添加线条将更容易看到动画期间的颜色变化

    df %>% plot_ly(x = ~x, y = ~y, frame = ~step, marker = list(color = ~col, size = 25, line = list(color = 'black',width = 5)),
                   mode = "markers", type = "scatter")
    

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2011-07-15
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多