【发布时间】: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