如何防止在Plotly中散布出淡出点?

时间:2019-10-31 00:03:09

标签: r ggplot2 plotly plotly-dash ggplotly

我正在为以下数据框的动画制作动画:

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

谢谢!

2 个答案:

答案 0 :(得分:0)

您只需从plotly()中删除ID属性即可。因此,plotly将停止在日期的开始和结束时寻找广告素材的确切值。

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

希望对您有帮助!!!

答案 1 :(得分:0)

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

在p $ x $ data [firstFrame] <-p $ x $ frames [[1]] $ data ...

您可以通过以下方式完成数据框

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

这将通过添加缺少日期/广告素材组合数据的行来完成数据框。我为这些添加的行提供了数据 Spend = 0 Profitability = 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个点开始。然后,一些点从左下角进入绘图。

我希望那是你想看到的。