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

Plotly Plot with Forecasting

Hi All,
I am trying the plotly with forecast plot in R studio now. I am getting plot with no forecast detail. the output is attached with source R code.

Herewith I am furnishing sample data source as below (my csv file contains as like sample records of 239 rows).

ReceivedDate Volume
07-04-2016 00:00 17290
08-04-2016 00:00 1539
09-04-2016 00:00 167
11-04-2016 00:00 10383
12-04-2016 00:00 3697
13-04-2016 00:00 3530
14-04-2016 00:00 1969
15-04-2016 00:00 1662
16-04-2016 00:00 3
17-04-2016 00:00 1
18-04-2016 00:00 23967
19-04-2016 00:00 574
20-04-2016 00:00 2098
21-04-2016 00:00 292
22-04-2016 00:00 1864
24-04-2016 00:00 22

I am using below code:

library(gdata)
library(plotly)
library(zoo)
library(forecast)
library(xts)
df <- read.csv(file=“D:/Users/senthil_d1/Desktop/Invdata.csv”, header=TRUE, sep=",")
dates=as.Date(df$ReceivedDate,"%Y-%m-%d")
xs=xts(df$Volume,dates)
#plot(xs)
fc <- forecast(xs)
autoplot(fc)

fc <- as.data.frame(fc)
fc <- tibble::rownames_to_column(fc)
colnames(fc) <- c(“date”, “forecast”, “low80”, “high80”, “low95”, “high95”)
fc$date <- as.yearmon(fc$date, format="%b %Y")

native plotly

plot forecast data

p <- plot_ly(fc, x = ~as.Date(date), y = ~forecast, type = ‘scatter’, mode = ‘lines’,
line = list(color=‘rgb(0,100,80)’),
name = ‘Average’) %>%
add_trace(y = ~high80, type = ‘scatter’, mode = ‘lines’,
line = list(color = ‘transparent’),
showlegend = FALSE, name = ‘High 80’) %>%
add_trace(y = ~low80, type = ‘scatter’, mode = ‘lines’,
fill = ‘tonexty’, fillcolor=‘rgba(0,100,80,0.2)’, line = list(color = ‘transparent’),
showlegend = FALSE, name = ‘Low 90’) %>%
add_trace(y = ~high95, type = ‘scatter’, mode = ‘lines’,
line = list(color = ‘transparent’),
showlegend = FALSE, name = ‘High 95’) %>%
add_trace(y = ~low95, type = ‘scatter’, mode = ‘lines’,
fill = ‘tonexty’, fillcolor=‘rgba(0,100,60,0.2)’, line = list(color = ‘transparent’),
showlegend = FALSE, name = ‘Low 95’)
p

Please correct me where I am wrong.
Can you please help me out on this,
Regards,
Senthil D

Hi @senthil_d1,

Perhaps check your date data (fc$date), it is likely that you have NA values in your date column after using the xts and forecast function.