在 R 中使用 dygraph 绘制预测图
To plot forecast using dygraph in R
我只是将预测数据复制到不同的数据框中,数据看起来像。
数据
`Date GMT` Last `Last(Lo).80` `Last(Hi).80`
1 2017-09-23 1650.000 0.000 0.000
2 2017-09-25 1650.000 0.000 0.000
3 2017-09-26 1655.000 0.000 0.000
4 2017-09-27 1653.000 0.000 0.000
5 2017-09-28 1652.210 1650.763 1653.657
6 2017-09-29 1652.714 1649.952 1655.477
7 2017-09-30 1653.238 1648.940 1657.536
8 2017-10-01 1654.264 1648.251 1660.278
代码
s <-subset(d1, select = c(1,5,6,7))
dygraph(s, main ="Price chart") %>%
dyRangeSelector() %>%
dyRangeSelector(height = 40,
dateWindow = c("2017-09-23", "2017-10-01")) %>%
dySeries(name = "actuals", label = "actual") %>%
dySeries(c(2,3,4), label = "Predicted") %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyOptions(axisLineColor = "navy", gridLineColor = "grey")
虽然 运行 我收到不支持数据的错误
将您的数据作为时间序列对象输入 dygraph
。
d1 <- read.table(text="
'Date GMT' 'Last' 'Last(Lo).80' 'Last(Hi).80'
1 2017-09-23 1650.000 0.000 0.000
2 2017-09-25 1650.000 0.000 0.000
3 2017-09-26 1655.000 0.000 0.000
4 2017-09-27 1653.000 0.000 0.000
5 2017-09-28 1652.210 1650.763 1653.657
6 2017-09-29 1652.714 1649.952 1655.477
7 2017-09-30 1653.238 1648.940 1657.536
8 2017-10-01 1654.264 1648.251 1660.278
", header=T)
d1$Date.GMT <- as.POSIXct(d1$Date.GMT, format = "%Y-%m-%d", tz="GMT")
library(tseries)
ts1 <- irts(time=d1[,1], value=as.matrix(d1[,2:4]))
library(dygraphs)
dygraph(ts1, main ="Price chart") %>%
dyRangeSelector() %>%
dyRangeSelector(height = 40,
dateWindow = c("2017-09-23", "2017-10-01")) %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyOptions(axisLineColor = "navy", gridLineColor = "grey")
我只是将预测数据复制到不同的数据框中,数据看起来像。
数据
`Date GMT` Last `Last(Lo).80` `Last(Hi).80`
1 2017-09-23 1650.000 0.000 0.000
2 2017-09-25 1650.000 0.000 0.000
3 2017-09-26 1655.000 0.000 0.000
4 2017-09-27 1653.000 0.000 0.000
5 2017-09-28 1652.210 1650.763 1653.657
6 2017-09-29 1652.714 1649.952 1655.477
7 2017-09-30 1653.238 1648.940 1657.536
8 2017-10-01 1654.264 1648.251 1660.278
代码
s <-subset(d1, select = c(1,5,6,7))
dygraph(s, main ="Price chart") %>%
dyRangeSelector() %>%
dyRangeSelector(height = 40,
dateWindow = c("2017-09-23", "2017-10-01")) %>%
dySeries(name = "actuals", label = "actual") %>%
dySeries(c(2,3,4), label = "Predicted") %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyOptions(axisLineColor = "navy", gridLineColor = "grey")
虽然 运行 我收到不支持数据的错误
将您的数据作为时间序列对象输入 dygraph
。
d1 <- read.table(text="
'Date GMT' 'Last' 'Last(Lo).80' 'Last(Hi).80'
1 2017-09-23 1650.000 0.000 0.000
2 2017-09-25 1650.000 0.000 0.000
3 2017-09-26 1655.000 0.000 0.000
4 2017-09-27 1653.000 0.000 0.000
5 2017-09-28 1652.210 1650.763 1653.657
6 2017-09-29 1652.714 1649.952 1655.477
7 2017-09-30 1653.238 1648.940 1657.536
8 2017-10-01 1654.264 1648.251 1660.278
", header=T)
d1$Date.GMT <- as.POSIXct(d1$Date.GMT, format = "%Y-%m-%d", tz="GMT")
library(tseries)
ts1 <- irts(time=d1[,1], value=as.matrix(d1[,2:4]))
library(dygraphs)
dygraph(ts1, main ="Price chart") %>%
dyRangeSelector() %>%
dyRangeSelector(height = 40,
dateWindow = c("2017-09-23", "2017-10-01")) %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyOptions(axisLineColor = "navy", gridLineColor = "grey")