Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

How to plot forecast using plotly for time series data having Index with Datetime value

Hi ,

I want to plot forecast of a time series using plotly. But my index of the timeseries consists of timestamp [Date Time] Values. which makes difficult to plot the forecast part.

head(qxts) #qxts is my time series data
[,1]
2017-04-25 16:52:00 -0.4120000
2017-04-25 16:53:00 -0.4526667
2017-04-25 16:54:00 -0.4586667
2017-04-25 16:55:00 -0.4606667
2017-04-25 16:56:00 -0.5053333
2017-04-25 16:57:00 -0.5066667

forecasting my time series

fit.xts <- auto.arima(qxts)
fore.xts <- forecast(fit.xts, h=10)

plotting the forecasting

plot_ly() %>%
add_lines(x = ~time(qxts), y = ~qxts,
color = I(“black”), name = “observed”) %>%
add_ribbons(x = time(fore.xts$mean), ymin = fore.xts$lower[, 2], ymax = fore.xts$upper[, 2],
color = I(“gray95”), name = “95% confidence”) %>%
add_ribbons(x = time(fore.xts$mean), ymin = fore.xts$lower[, 1], ymax = fore.xts$upper[, 1],
color = I(“gray80”), name = “80% confidence”) %>%
add_lines(x = time(fore.xts$mean), y = fore.xts$mean, color = I(“blue”), name = “prediction”)

This gives empty plot. Can someone help me? I have been looking for answers for two days. Thank you.

@dhinar1991 can you please share the data as well?

Hi carson,

Thanks for the reply. I already plotted and rectified the error using the codes below.

library(forecast)

qxts = structure(list(y=c(-0.4120000005, -0.452666665083333, -0.458666667316667, -0.460666666416667, -0.505333344133333, -0.506666670233333, -0.50999999045, -0.498666668933333, -0.502666672066667, -0.505333344133333, -0.50200000405, -0.506666680166667, -0.508000006266667, -0.5040000081, -0.501333341, -0.50200000405, -0.502000009016667, -0.504000003133333, -0.50466667115, -0.505333344133333, -0.501333341, -0.503333340083333, -0.496666664883333, -0.5040000081, -0.49799999595, -0.389333337533333, -0.3640000075, -0.365333338566667, -0.3586666733, -0.3599999994, -0.355333338183333, -0.365333338566667, -0.360666667416667, -0.3640000075, -0.3546666652, -0.369999999783333, -0.3653333336, -0.36733333765, -0.396666670833333, -0.469333335766667, -0.4586666673, -0.4639999916, -0.457333331283333, -0.4646666596, -0.4680000047, -0.4687999964, -0.467333336683333, -0.460666661433333, -0.4653333326, -0.4720000029, -0.453333328166667, -0.466000000616667, -0.4646666646, -0.45933333535, -0.464666659633333, -0.4579999993, -0.464666669566667, -0.473999996983333, -0.417333334666667, -0.46400000155, -0.4599999934, -0.46866666775, -0.47200000288, -0.465333337583333, -0.4719999979, -0.462666660516667, -0.46240000126, -0.465333322666667, -0.4673333317, -0.459999993416667, -0.464666659616667, -0.461333329466667, -0.46599999565, -0.461999997483333, -0.461999992516667, -0.466666663683333, -0.457333336283333, -0.46600000065, -0.463999996566667, -0.46400000155, -0.488666658566667, -0.543333326766667, -0.489333341533333, -0.3600000143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.3573333323, -0.464666662133333, -0.3360000037, -0.445333333066667, -0.49333334465, -0.444666661316667, -0.325333339483333, -0.60199998815, -0.33039999454, -0.394666674216667, -0.406666671233333, -0.4066666588, -0.52533333005, -0.552000006066667, -0.6973333458, -0.526000003, -0.565333336583333, -0.451428575185714, -0.3973333314, -0.591999994871429, -0.49120000004, -0.4313333432, -0.197333325966667, -0.545999998833333, -0.49600000976, -0.4595000035875, -0.45920000072, -0.453714281285714, -0.455333337166667, -0.461333334433333, -0.461999992533333, -0.457333331283333, -0.4606666664, -0.456000005183333, -0.4593333254, -0.4579999993, -0.45679999586, -0.455428570485714, -0.454666664183333, -0.453999991183333, -0.452666665083333, -0.464000001533333, -0.456666668216667, -0.45439999696, -0.45666666825, -0.454666664166667, -0.459999998383333, -0.45840000508, -0.459333330366667, -0.394666661816667, -0.367333332983333, -0.2823999971, -0.3446666648, -0.464666659633333, -0.443333327766667, -0.33920000494, -0.332666660333333, -0.363333340716667, -0.43599999546, -0.518000001716667, -0.15933333585, -0.2493333357, -0.43359999356, -0.462666669166667, -0.410000008833333, -0.357333337883333, -0.3615999937, -0.494666665783333, -0.52200000485, -0.29400000225, -0.4527999997, -0.474000004416667, -0.342666663233333, -0.53680000306, -0.423333331933333, -0.498666671416667, -0.439333344516667, -0.46480000914, -0.368666668733333, -0.2673333399, -0.303333333366667, -0.21279999912, -0.398000004383333, -0.444666676216667, -0.63679999114, -0.337333336466667, -0.2599999905 ),
date = c(1493139120, 1493139180, 1493139240, 1493139300, 1493139360, 1493139420, 1493139480, 1493139540, 1493139600, 1493139660, 1493139720, 1493139780, 1493139840, 1493139900, 1493139960, 1493140020, 1493140080, 1493140140, 1493140200, 1493140260, 1493140320, 1493140380, 1493140440, 1493140500, 1493140560, 1493140620, 1493140680, 1493140740, 1493140800, 1493140860, 1493140920, 1493140980, 1493141040, 1493141100, 1493141160, 1493141220, 1493141280, 1493141340, 1493141400, 1493141460, 1493141520, 1493141580, 1493141640, 1493141700, 1493141760, 1493141820, 1493141880, 1493141940, 1493142000, 1493142060, 1493142120, 1493142180, 1493142240, 1493142300, 1493142360, 1493142420, 1493142480, 1493142540, 1493142600, 1493142660, 1493142720, 1493142780, 1493142840, 1493142900, 1493142960, 1493143020, 1493143080, 1493143140, 1493143200, 1493143260, 1493143320, 1493143380, 1493143440, 1493143500, 1493143560, 1493143620, 1493143680, 1493143740, 1493143800, 1493143860, 1493143920, 1493143980, 1493144040, 1493144100, 1493144160, 1493144220, 1493144280, 1493144340, 1493144400, 1493144460, 1493144520, 1493144580, 1493144640, 1493144700, 1493144760, 1493144820, 1493144880, 1493144940, 1493145000, 1493145060, 1493145120, 1493145180, 1493145240, 1493145300, 1493145360, 1493145420, 1493145480, 1493145540, 1493145600, 1493145660, 1493145720, 1493145780, 1493145840, 1493145900, 1493145960, 1493146020, 1493146080, 1493146140, 1493146200, 1493146260, 1493146320, 1493146380, 1493146440, 1493146500, 1493146560, 1493146620, 1493146680, 1493146740, 1493146800, 1493146860, 1493146920, 1493146980, 1493147040, 1493147100, 1493147160, 1493147220, 1493147280, 1493147340, 1493147400, 1493147460, 1493147520, 1493147580, 1493147640, 1493147700, 1493147760, 1493147820, 1493147880, 1493147940, 1493148000, 1493148060, 1493148120, 1493148180, 1493148240, 1493148300, 1493148360, 1493148420, 1493148480, 1493148540, 1493148600, 1493148660, 1493148720, 1493148780, 1493148840, 1493148900, 1493148960, 1493149020, 1493149080, 1493149140, 1493149200, 1493149260, 1493149320, 1493149380, 1493149440, 1493149500, 1493149560, 1493149620, 1493149680, 1493149740, 1493149800, 1493149860, 1493149920, 1493149980, 1493150040, 1493150100, 1493150160, 1493150220, 1493150280, 1493150340, 1493150400, 1493150460, 1493150520))
)

fit.xts <- auto.arima(qxts$y)
forecast_length <- 10
fore.xts <- forecast(fit.xts, h=forecast_length)

fore.dates <- seq(as.POSIXct(qxts$date[length(qxts$date)], origin=‘1970-01-01’), by=qxts$date[length(qxts$date)] - qxts$date[length(qxts$date)-1], len=forecast_length)

p <- plot_ly() %>%
add_lines(x = as.POSIXct(qxts$date, origin=‘1970-01-01’), y = qxts$y,
color = I(“black”),
name = “observed”,
marker=list(mode=‘lines’)) %>%
add_lines(x = fore.dates, y = fore.xts$mean, color = I(“blue”), name = “prediction”) %>%
add_ribbons(x = fore.dates,
ymin = fore.xts$lower[, 2],
ymax = fore.xts$upper[, 2],
color = I(“gray95”),
name = “95% confidence”) %>%
add_ribbons(p,
x = fore.dates,
ymin = fore.xts$lower[, 1],
ymax = fore.xts$upper[, 1],
color = I(“gray80”), name = “80% confidence”)

p

Regards,Dhivya.