我有两个每周系列的数据集。下面您可以看到数据代码,还可以看到plotly的代码。
# Code
library(plotly)
library(data.table)
library(tidyr)
library(dplyr)
library(tidyverse)
ARTEFICIAL_DATA<-data.frame(structure(list(week = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18), `2019 Series_1` = c(1286, 838, 1537,
880, 1090, 1277, 1104, 622, 405, 729, 863, 716, 648, 1042, 765,
913, 684, 615), `2020 Series_1` = c(559, 456, 1348, 1083, 930,
1068, 1024, 691, 747, 635, 740, 665, 465, 442, 437, 764, 378,
392)), row.names = c(NA, -18L), class = c("tbl_df", "tbl", "data.frame"
)))
WEEKLY_TOTAL_REVENUE_PLOTLY<-ARTEFICIAL_DATA%>%
select(week,`2019 Series_1`,`2020 Series_1`)
dat <-as.data.table(WEEKLY_TOTAL_REVENUE_PLOTLY)
colnames(dat) <- c('week', 'series1', 'series2')
fig <- plot_ly(dat, x = ~week, y = ~series2, name = "2019", type = 'scatter', mode = 'lines',
line = list(dash = "dash"))
fig <- fig %>% add_trace(y = ~series1, name = "2020", line = list(dash = "solid"))%>%
layout(
xaxis = list(title = 'week'),
yaxis = list(title = 'Dollars')
)
fig
因此,这行代码生成的图形如下图所示。
但是我的意图是使累积行像下面的示例。
所以有人可以帮助我如何使用Plotly绘制上述示例吗?
您将必须转换数据以具有累积总和:
dat <- dat %>% mutate(series1 = cumsum(series1),series2 = cumsum(series2))
fig <- plot_ly(dat, x = ~week, y = ~series2, name = "2019", type = 'scatter', mode = 'lines',
line = list(dash = "dash"))
fig <- fig %>% add_trace(y = ~series1, name = "2020", line = list(dash = "solid"))%>%
layout(
xaxis = list(title = 'week'),
yaxis = list(title = 'Dollars')
)
fig