我创建了2个剧情动画(p1& p2),显示了Revenue&销售额逐年增长。两种动画在单独使用时都能很好地发挥作用,
但是,我使用subplot()在同一页面上显示两个动画,但不知何故只显示轴没有动画。
不确定我哪里出错了,非常感谢任何帮助!
我的代码:
# function to plot
plots<-function(x){yr_plots<-melt(aggregate(.~Year,x,FUN = sum),id.vars =
"Year")%>%
accumulate_by(~Year)
p<-yr_plots %>%
plot_ly(
x = ~Year,
y = ~value,
split = ~variable,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
) %>%
layout(title = "Yearly Growth",
xaxis = list(title = "Year",zeroline = F),
yaxis = list(title = "",zeroline = F)
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 0.5, xanchor = "right", y = 1, yanchor = "middle"
)
p
}
# Animated plot of Revenue
p1<-plots(test1[,c(1,2,4,6,8)])
# Animated plot of Sales
p2<-plots(test1[,c(1,3,5,7,9)])
# Link Animated views
subplot(p1, p2, nrows = 1, widths = c(0.5, 0.5), titleX = TRUE) %>%
hide_legend() %>%
animation_opts(1000, redraw = FALSE)
示例数据:
test1<-structure(list(Year = c(1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L,
1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L,
1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L,
1991L), Revenue_Res = c(167998, 851685, 6646479, 1038460, 222545,
2233265, 821178, 3260358, 744998, 1587467, 1379761, 1982482,
1592729, 183019, 184784, 356128, 318672, 4414190, 1124494, 3519366,
1305102, 1636627, 302855, 167609, 1087158, 102694, 171087, 891553,
7120397, 1097438, 243344, 2264007, 866430, 3549079, 777383, 1595960,
1602645, 2157926, 1727704, 199218, 192380, 348362, 332973, 4689927,
1077659, 3794766, 1350524, 1671412, 317478, 169922, 1168339,
109223), Sales_Res = c(1661309, 10557865, 66574670, 10375882,
2651447, 29932836, 10513101, 32871442, 9514697, 21433524, 19101665,
25318851, 21652353, 3358385, 2954024, 3444196, 3565533, 38574309,
17077367, 38164153, 18257716, 28756837, 4246106, 1808617, 16385267,
1719932, 1602778, 11000634, 66016701, 10440513, 2824340, 30186630,
11159189, 35963793, 9933309, 21576815, 20295448, 26759510, 23386303,
3458894, 3096206, 3356706, 3665111, 39177424, 15324940, 39597641,
18706763, 29605275, 4460041, 1783450, 17349466, 1819414), Revenue_Coml =
c(177645,
422196, 7541253, 942670, 159717, 1676868, 421538, 2464895, 609175,
973398, 701543, 1678551, 1192964, 128107, 115688, 191075, 363175,
4911434, 668062, 2359834, 733904, 738774, 282529, 126186, 733469,
112469, 185865, 437986, 7876227, 1001134, 170122, 1709681, 443932,
2632023, 632033, 981886, 749556, 1756558, 1220603, 140943, 119932,
194741, 373237, 5098819, 643299, 2539487, 759906, 734742, 288399,
133098, 773960, 120033), Sales_Coml = c(1972116, 6075078, 79691298,
10342476, 2310713, 22867547, 6727380, 31734448, 9169289, 13814111,
10452013, 20609560, 18468963, 2737526, 1794510, 2010427, 4464123,
46921091, 11633982, 29159184, 11926736, 12127835, 4514613, 1484125,
12697933, 2175802, 2005246, 6300360, 78424653, 10544096, 2419529,
23240749, 7123124, 33118782, 9551119, 13970338, 10667414, 21454733,
19111774, 2818850, 1875987, 2029125, 4532875, 46981962, 10586713,
30552516, 12208945, 12096992, 4733977, 1491488, 13308794, 2310466
), Revenue_Indl = c(36308, 516152, 4071492, 460626, 147636, 1290544,
453290, 2121723, 399867, 1082569, 984268, 2051269, 639878, 187147,
84329, 255513, 219615, 1846248, 427321, 2747622, 1031925, 1656024,
219380, 91377, 774243, 268441, 38275, 530630, 4259687, 461498,
153185, 1300456, 468565, 2179892, 410015, 1102231, 1069135, 2061462,
642814, 193272, 85736, 242788, 219734, 1917863, 439794, 2812918,
1055633, 1606856, 226080, 97504, 793317, 261504), Sales_Indl = c(459283,
10126207, 55891513, 6099932, 3271591, 26717324, 11392481, 39298520,
8087146, 25861968, 19308301, 35062021, 12937381, 6529327, 1759713,
3418392, 4412882, 31929348, 11763695, 45991880, 24701256, 35312602,
5766212, 1381173, 19405056, 7729470, 465877, 10517511, 56191046,
5822465, 3240945, 27193099, 11684147, 39712089, 8283740, 26583901,
19448114, 35006635, 13113854, 6622098, 1762079, 3265025, 4546441,
31111531, 11414862, 44728178, 25360811, 35667295, 5875923, 1389670,
19685676, 7498103), Revenue_Total = c(403247, 1832979, 18663604,
2489109, 535072, 5275470, 1744626, 8360944, 1784908, 3830174,
3121423, 5849651, 3484571, 519544, 403293, 816302, 981068, 12119202,
2327991, 8781831, 3113292, 4096453, 841129, 390304, 2640811,
494945, 417259, 1902619, 19661146, 2608969, 572261, 5352594,
1828987, 8913300, 1850716, 3843787, 3482287, 6094851, 3653390,
555447, 417569, 800075, 1004892, 12664493, 2277704, 9310545,
3211345, 4083092, 868558, 405872, 2781595, 499291), Sales_Total = c(4253839,
27365296, 211092921, 27187240, 8283868, 80440115, 29437380, 111576774,
27148766, 63825890, 49533729, 82366870, 53924638, 13124583, 7013849,
8979614, 13820760, 129324439, 42504220, 114750660, 55651790,
77144770, 15401673, 4716230, 49198027, 11768838, 4255712, 28440256,
208650490, 27171404, 8535865, 81539086, 30781462, 116868559,
28152224, 64704299, 51107809, 84518542, 56514157, 13406599, 7255202,
8761774, 14083945, 129410968, 39404397, 116336997, 57069331,
78390143, 15907404, 4704455, 51032208, 11756847)), row.names = c(1L,
3L, 5L, 7L, 9L, 11L, 13L, 15L, 17L, 19L, 21L, 23L, 25L, 27L,
29L, 31L, 33L, 35L, 37L, 39L, 41L, 43L, 45L, 47L, 49L, 51L, 52L,
54L, 56L, 58L, 60L, 62L, 64L, 66L, 68L, 70L, 72L, 74L, 76L, 78L,
80L, 82L, 84L, 86L, 88L, 90L, 92L, 94L, 96L, 98L, 100L, 102L), class =
"data.frame")
答案 0 :(得分:0)
你试过这个吗?
library(plotly)
library(reshape2)
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)
}
plots<-function(x){yr_plots<-melt(aggregate(.~Year,x,FUN = sum),id.vars =
"Year")%>%
accumulate_by(~Year)
p<-yr_plots %>%
plot_ly(
x = ~Year,
y = ~value,
split = ~variable,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
) %>%
layout(title = "Yearly Growth",
xaxis = list(title = "Year",zeroline = F),
yaxis = list(title = "",zeroline = F)
)
p
}
# Animated plot of Revenue
p1<-plots(test1[,c(1,2,4,6,8)])
# Animated plot of Sales
p2<-plots(test1[,c(1,3,5,7,9)])
# Link Animated views
subplot(p1, p2, nrows = 1, widths = c(0.5, 0.5), titleX = TRUE) %>%
hide_legend() %>%
animation_opts(1000,
frame = 1100,
redraw = FALSE) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 0.5, xanchor = "right", y = 1, yanchor = "middle"
)