我使用了一个预测包,并在plotly中绘制了数据图,现在我需要使用ggplot代替plotly。现在我需要用ggplot代替plotly。我找到了这个 编码我做了一个在ggplot中绘制预测的函数,但我无法使代码工作。另外,我知道预测中的自动绘制函数使用ggplot,但它限制了自定义。
以下是我的Plotly运行代码
library(ggplot2)
library(forecast)
library(plotly)
df<-structure(list(Date = structure(c(18316, 18317, 18318, 18319,
18320, 18321, 18322, 18323, 18324, 18325, 18326, 18327, 18328,
18329, 18330, 18331, 18332, 18333, 18334, 18335, 18336, 18337,
18338, 18339, 18340, 18341, 18342, 18343, 18344, 18345, 18346,
18347, 18348, 18349, 18350, 18351, 18352, 18353, 18354, 18355,
18356, 18357, 18358, 18359, 18360, 18361, 18362, 18363, 18364,
18365, 18366, 18367, 18368, 18369, 18370, 18371, 18372, 18373,
18374, 18375, 18376, 18377, 18378, 18379, 18380, 18381, 18382,
18383, 18384, 18385, 18386, 18387, 18388, 18389, 18390, 18391,
18392, 18393, 18394, 18395, 18396, 18397, 18398, 18399, 18400,
18401, 18402, 18403, 18404, 18405, 18406, 18407, 18408, 18409,
18410), class = "Date"), Count = c(5L, 11L, 26L, 43L, 45L, 45L,
46L, 56L, 56L, 56L, 57L, 57L, 60L, 63L, 63L, 67L, 67L, 75L, 95L,
97L, 103L, 111L, 118L, 127L, 130L, 137L, 149L, 158L, 159L, 152L,
152L, 159L, 168L, 171L, 188L, 194L, 216L, 237L, 261L, 335L, 385L,
456L, 561L, 637L, 743L, 798L, 869L, 1020L, 1091L, 1148L, 1176L,
1196L, 1296L, 1395L, 1465L, 1603L, 1619L, 1657L, 1792L, 1887L,
1986L, 2217L, 2249L, 2254L, 2241L, 2327L, 2459L, 2745L, 2883L,
3169L, 3291L, 3732L, 4028L, 4142L, 4695L, 4952L, 5901L, 6314L,
7101L, 7683L, 8436L, 9124L, 9852L, 10645L, 11234L, 11962L, 12559L,
13275L, 13911L, 14569L, 15029L, 15181L, 15097L, 15146L, 15229L
)), class = "data.frame", row.names = c(NA, -95L))
# frequency here in days
tm<-ts(df$Count,frequency = 365.25 )
fit.xts <- auto.arima(tm,approximation=FALSE,stepwise=FALSE)
forecast_length <- 60
fore.xts <- forecast(fit.xts, h=forecast_length)
#formating the forecasting date
fore.dates <- seq(df$Date[length(df$Date)], by=df$Date[length(df$Date)] -
df$Date[length(df$Date)-1], len=forecast_length)
plot_ly() %>%
add_lines(x = df$Date, y = tm,
color = I("black"),
name = "Observed",
marker=list(mode='lines')) %>%
# this will add line of prediction, which looks linear ! strange !
# what's the alternative of mean value?
add_lines(x = fore.dates, y = fore.xts$mean, color = I("blue"), name =
"Prediction") %>%
add_ribbons(x = fore.dates,
#this prints even negative values of prediction !
ymin = fore.xts$lower[, 2],
ymax = fore.xts$upper[, 2],
color = I("gray90"),
name = "95% confidence") %>%
layout(legend = list(orientation = "h", # show entries horizontally
xanchor = "center", # use center of legend as anchor
x = 0.5))
有什么建议吗?
这将是一个选项(创建预测日期并将其与存储在 fore.xts
):
Date <- seq(max(qxts$Date) + 1, max(qxts$Date) + 60, "day")
forecast_point <- fore.xts$mean
forecast_lower <- fore.xts$lower[,2]
forecast_upper <- fore.xts$upper[,2]
forecast_df <- tibble(Date, forecast_point, forecast_lower, forecast_upper)
#without confidence intervals
qxts %>%
ggplot(aes(x = Date, y = y)) +
geom_point() +
geom_line() +
geom_line(data = forecast_df, aes(x = Date, y = forecast_point), color = "blue")
#with confidence intervals
qxts %>%
ggplot(aes(x = Date, y = y)) +
geom_point() +
geom_line() +
geom_smooth(data = forecast_df, aes(x = Date, y = forecast_point, ymax = forecast_upper, ymin = forecast_lower), stat = "identity")
这是你要找的吗?