Black Lives Matter. Please consider donating to Black Girls Code today.

Forecasting time series data in plotly gives empty and wrong plot

I have a data frame “gg”, that looks like this:

head(gg)
timestamps value
1 2017-04-25 16:52:00 -0.4120000
2 2017-04-25 16:53:00 -0.4526667
3 2017-04-25 16:54:00 -0.4586667
4 2017-04-25 16:55:00 -0.4606667
5 2017-04-25 16:56:00 -0.5053333
6 2017-04-25 16:57:00 -0.5066667

I need to plot this as a Time series data to do forecasting.

gg$timestamps <- as.POSIXct(gg$timestamps, format = “%Y-%m-%d %H-%M-%S”) #changing “Timestamps” column ‘factor’ to ‘as.POSIXct’.

gg.ts <- xts(x=gg$value, order.by = gg$timestamps) #converting the dataframe to time series (Non Regular Time series)

fitting <- auto.arima(gg.ts) #fitting the time series model using auto.arima

fore <- forecast(fitting, h=30, level = c(80,95)) #Forecasting

I am using plotly to this forecast model (Inspired from here : https://plot.ly/r/graphing-multiple-chart-types/#plotting-forecast-objects)

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

The plot comes out wrong: 1) x axis labels are wrong. Its not showing the timestamp value on x axis 2) the plot is also not coming out.

Please help me. Thank you in advance. Regards,Dhivya

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