forked from jaNGOB/twittermoney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation plot.R
76 lines (67 loc) · 1.98 KB
/
Animation plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
library(plotly)
library(quantmod)
df2 <- data.frame(merge(topIndexReturn,IndicesNormReturn[,1]))
df2$ID2 <- seq.int(nrow(df2))
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
df2 <- df2 %>% accumulate_by(~ID2)
fig <- df2 %>% plot_ly(
x = ~ID2,
y = ~Long.Short.Strategy,
frame = ~frame,
type = 'scatter',
mode = 'lines',
#fill = 'tozeroy',
#fillcolor='rgba(29, 161, 242, 0.5)',
line = list(color = 'rgb(29, 161, 242)'),
text = ~paste("Day:", ID2, "Date:", index(topIndexReturn), "Index Level:", round(topIndexReturn, 2)),
hoverinfo = 'text',
name = "Twitter Index"
)
fig <- fig %>% add_trace(x = ~ID2, y = ~Market.Index, frame = ~frame,
type = 'scatter',
mode = 'lines',
#fill = 'tozeroy',
#fillcolor='rgba(220,20,60, 0.5)',
line = list(color = 'rgb(220,20,60)'),
text = ~paste("Day:", ID2, "Date:", index(IndicesNormReturn[,1]), "Index Level:", round(IndicesNormReturn[,1], 2)),
hoverinfo = 'text',
name = "Market Index")
fig <- fig %>% layout(
title = "Twitter Strategy",
yaxis = list(
title = "Index",
range = c(60,120),
zeroline = F
),
xaxis = list(
title = "Day",
range = c(0,239),
zeroline = F,
showgrid = F
)
)
fig <- fig %>% animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
)
fig <- fig %>% animation_slider(
currentvalue = list(
prefix = "Day "
),
activebgcolor = 'rgba(29, 161, 242, 0.5)',
bgcolor = 'rgb(29, 161, 242)',
offset = 7
)
fig <- fig %>% animation_button(
label = "Start",
bgcolor = 'rgb(29, 161, 242)'
)
fig