r-如何防止Plotly中散点的淡出



我正在尝试为以下数据帧设置一个绘图动画:

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

谢谢!

您只需从plotly((中删除ID属性即可。所以情节会停止寻找创意的确切价值的开始和结束日期。

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

我希望这有帮助!!!!

正如您所说,问题是您的数据帧不完整。因此,您应该看到一条错误消息,如:

在p$x$数据中[firstFrame]<-p$x$帧[[1]]$数据。。。

您可以通过完成数据帧

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

这将通过添加缺少日期/创意组合数据的行来完成数据帧。我已经为这些添加的行提供了数据支出=0盈利能力=0。通过将Y轴限制设置为-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个点开始。然后,一些点从左下角进入绘图。

我希望这是你想要看到的。

相关内容

  • 没有找到相关文章