【问题标题】:How to prevent fading out of scatter points in Plotly?如何防止 Plotly 中散点的淡出?
【发布时间】:2020-02-26 08:01:41
【问题描述】:

我正在尝试为以下数据框绘制动画:

df <- data.frame(
  Creative <- c("BG", "HB", "OV3", "OV4", "TI", 
                "BG", "HB", "IW", "OV3", "OV4", "TI", 
                "Women30", "BG", "HB", "IW", "LA", 
                "OP3", "OV4", "TI", "BG", "HB", "IW", 
                "OV3", "OV4", "TI", "TM", "BG", "HB", "IW",
                "OV3", "OV4", "TI", "BG", "HB", "IW", "OV3", 
                "OV4", "TI", "Hb", "IW", "OV3", "OV4", "TI", "TM"),
  Spend <- c("10000", "3000", "4000", "16000", "10000", "10000", "7000", "5000", "10000", 
             "20000", "14000", "2000", "11000", "7000", "6000", "3000", "12000", "20000", "12000",
             "8000", "14000", "7000", "8100", "15505", "10075.5", "2000.62", "7000.14", "10531.08", "14831.03", "3481.73",
             "5031.93", "14600.53", "8000.12", "15000.08", "29000.79", "5000.65", "40000.04", "14000.75", "7000.56", "23000.64", "10000.55", 
             "12000.56", "11353.7", "8000.65"),
  Profitability <- c("0.18911111", "0.09", "-0.04", "-0.08", "0.01799735", "0.05067851", "0.29", "0.11817021", "-0.03682584", "-0.16",
                     "-0.11639344", "-0.41", "0.07035971", "0.34", "0.31012579", "-0.21522822", "-0.03106155", "-0.19", "-0.12512605",
                     "-0.05311224", "0.18", "-0.09731278", "-0.20401676", "-0.05457789", "-0.03661734", "-0.17182222", "-0.068125",
                     "0.14", "0.24371284", "-0.02686726", "-0.08510383", "-0.09900938", "-0.09861953", "0.05", "0.22176382", "0.07751868",
                     "0.05005076", "-0.13422111", "-0.17", "0.22727374", "0.10032397", "0.06960388", "-0.28228181", "0.05402597"),
  Date <- c("09/27/19", "09/27/19", "09/27/19", "09/27/19", "09/27/19", "10/01/19", "10/01/19", "10/01/19", "10/01/19",
            "10/01/19", "10/01/19", "10/01/19", "10/02/19", "10/02/19", "10/02/19", "10/02/19", "10/02/19", "10/02/19", 
            "10/02/19", "10/08/19", "10/08/19", "10/08/19", "10/08/19", "10/08/19", "10/08/19", "10/08/19", "10/10/19", 
            "10/10/19", "10/10/19", "10/10/19", "10/10/19", "10/10/19", "10/11/19", "10/11/19", "10/11/19", "10/11/19", 
            "10/11/19", "10/11/19", "10/14/19", "10/14/19", "10/14/19", "10/14/19", "10/14/19", "10/14/19")
)

colnames(df) <- c("Creative", "Spend", "Profitability", "Date")
df$Date <- as.Date(df$Date, format = "%m/%d/%y")
df[, 2:3] <- lapply(df[, 2:3], function(x) as.numeric(as.character(x)))

我在 x 轴上绘制支出图表,在 y 轴上绘制盈利能力图表,我希望将每个广告素材表示为一个散点。动画将随着时间的推移(迭代日期)。到目前为止,这是我的动画代码:

plot <- df %>%
  plot_ly(
    x = ~Spend,
    y = ~Profitability,
    frame = ~Date,
    color = ~Creative,
    ids = ~Creative,
    text = ~Creative,
    type = 'scatter',
    marker = list(size = 15),
    mode = 'markers+text',
    textposition='bottom',
    showlegend = F
  )

plot_anim <- plot %>%  animation_opts(
  1000, easing = "cubic-in-out", transition = "1000", redraw = FALSE, mode = "afterall"
) %>% 
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  ) %>%
  animation_slider(
    currentvalue = list(prefix = "DATE ", font = list(color="red"))
  )

我面临的问题与并非所有广告素材都出现在每个日期有关(因为他们没有在那天花费)。在这种情况下,Plotly 使某一天存在而不是下一天的散点消失,但这会使图形混乱,因为它分散了散点动画的注意力,这些散点实际上是从一个点过渡到另一个点图表。有没有办法改变这种情况,如果第二天不存在散点,它就会消失,而其他点可以有正常的连续过渡(这似乎是 ggplot 上的默认方法,但不是 plotly 上的)。

作为参考,这是我试图在 ggplot 中绘制的图(由于状态低,无法直接附加 gif)。使用 ggplot,您可以看到点在下一个时间步中不存在时只是消失而不是消失: ggplot gif

谢谢!

【问题讨论】:

    标签: r ggplot2 plotly plotly-dash ggplotly


    【解决方案1】:

    您可以从 plotly() 中删除 ID 属性。因此,plotly 将停止寻找 creative 的确切值作为日期的开始和结束。

    plot <- df %>%
       plot_ly(
        x = ~Spend,
        y = ~Profitability,
        frame = ~Date,
        color = ~Creative,
        text = ~Creative,
        type = 'scatter',
        marker = list(size = 15),
        mode = 'markers+text',
        textposition='bottom',
        showlegend = F
      )
    
    
    
    plot_anim <- plot %>%  animation_opts(
      1000, easing = "linear-in", transition = "1000", redraw = FALSE, mode = "afterall"
    ) %>% 
      animation_button(
        x = 1, xanchor = "right", y = 0, yanchor = "bottom"
      ) %>%
      animation_slider(
        currentvalue = list(prefix = "DATE ", font = list(color="red"))
      )
    
    plot_anim
    

    希望对你有帮助!!!!

    【讨论】:

      【解决方案2】:

      正如您所说,问题在于您的数据框不完整。因此,您应该看到如下错误消息:

      在 p$x$data[firstFrame]

      您可以通过

      完成数据框
      dfa <- df %>% complete(Date, nesting(Creative), fill = list(Spend = 0, Profitability =-0.6))
      

      这将通过添加缺少日期/创意组合数据的行来完成数据框。我已经为这些添加的行提供了数据 Spend = 0Profitability = 0。通过将 yaxis 限制设置为 -0.5 到 +0.5,您可以将这些点存储在图的左下角,它们将从那里移动到图中。 因此我添加了layout(yaxis = list(range = c(-0.5, 0.5)))

      还添加了style(textposition = 'bottom'),以确保文本标签正确显示。

      完整代码为:

      dfa <- df %>% complete(Date, nesting(Creative),fill = list(Spend = 0, Profitability =-0.6))
      plot <- dfa %>%
        plot_ly(
          x = ~Spend,
          y = ~Profitability,
          frame = ~Date,
          color = ~Creative,
          ids = ~Creative,
          text = ~Creative,
          type = 'scatter',
          marker = list(size = 15),
          mode = 'markers+text',
          #textposition='bottom',
          showlegend = F
        )
      
      plot_anim <- plot %>%  animation_opts(
        1000, easing = "cubic-in-out", transition = "1000", redraw = TRUE, mode = "afterall"
      ) %>% 
        animation_button(
          x = 1, xanchor = "right", y = 0, yanchor = "bottom"
        ) %>%
        animation_slider(
          currentvalue = list(prefix = "DATE ", font = list(color="red"))
        ) %>% 
        style(textposition = 'bottom') %>% 
        layout(yaxis = list(range = c(-0.5, 0.5)))
      
      plot_anim
      

      试试看,你会看到动画以 5 个点开始。然后一些点从左下角进入情节。

      我希望这是你想看到的。

      【讨论】:

        猜你喜欢
        • 2018-06-09
        • 2020-12-04
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        • 2021-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多