R中的时序时间序列预测-修改默认的x轴和y轴范围

时间:2018-07-01 16:49:22

标签: r plotly forecasting

我对使用plotly进行的时间序列预测有疑问。我已经建立了一个预测图表,但是几乎不需要更改,但是对我来说不起作用。请查找附件以获取详细信息,并请告诉我有关R代码的更正或建议。

查询:

  1. 当前X轴显示2,016.5,2017,2017.5等,但是我希望它显示 yearmonth ,例如2016.04,2014,05等。 请告知 yearmonth 是数据字段,请参阅附件数据

  2. 当前Y轴显示的标签之间的差异为50 K,但是我希望将其显示为5 K,10K,15K等。

下面是使用的R代码:

arr.sort(function (x, y) { return y.created - x.created || y.score - x.score; });

以下是示例数据:

library(forecast)
library(plotly)
ord <- order(ds$`Calendar Year-DISPLAY_KEY`,ds$`Calendar Month-DISPLAY_KEY`)
sds <- ds[ord,]
firstRec <- sds[1,]
mn <- as.numeric(firstRec$'Calendar Month-DISPLAY_KEY')    
yr <- as.numeric(as.character(firstRec$'Calendar Year-DISPLAY_KEY'))
tm <- ts(data = sds$Calc_Best_DSO , start= c(yr,mn) ,frequency = 12)
plot(tm)
tm[is.na(tm)] <-0

fit <- ets(tm)
fore <- forecast(fit, h = 3, level = c(80, 95))

plot_ly() %>%
  add_lines(x = time(tm), y = tm,hoverinfo = "text",
            color = I("black"), name = "observed",text= paste("Month: ",sds$`Calendar Month-DISPLAY_KEY`,
                    "<br>","Year: ",sds$`Calendar Year-DISPLAY_KEY`,
                   "<br>","DSO: ",sds$Calc_Best_DSO)) %>%
  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")

Data

Chart

2 个答案:

答案 0 :(得分:0)

最好以自定义方式自定义轴的方法是使用所需选项设置变量。

对于x轴,它看起来类似于下面的代码。

    a <- list(
       autotick = FALSE,
       tick0 = 0,
       dtick = 1)

这将仅在每间隔1的x轴上显示刻度线。我知道这不是您要的。使用dtick = .01,将使x轴难以阅读。

对于y轴,它看起来类似于下面的代码。

   b <- list(
      autotick = FALSE,
      tick0 = 0,
      dtick = 5000) #for 5k intervals

现在,您只需将它们输入到您的绘图代码中即可。这是一个例子。

    plot_ly()%>%
      add_lines(x = time(tm) y = tm, hoverinfo = text, color = I("black"), name = "Observed") %>%
      layout(xaxis = a, yaxis = b)

这应该有效。您需要做的就是定义a和b,然后将布局添加到当前的plotly代码中。

希望这会有所帮助。

答案 1 :(得分:0)

您要将日期格式设置为R date。这将有助于格式化。

library(plotly)
x <- read.table(
  text = 'Month,Year,YearMonth,Population
1,2017,201701,100
  1,2018,201801,300
  2,2018,201802,310
  3,2018,201803,320
  4,2018,201804,330
  2,2017,201702,200
  3,2017,201703,300
  4,2017,201704,400
  5,2017,201705,500
  6,2017,201706,600
  7,2017,201707,700
  8,2017,201708,800
  9,2017,201709,900
  10,2017,201710,1000
  11,2017,201711,1100
  12,2017,201712,1200', 
  header = TRUE, sep = ','
)
x$YearMonth <- as.Date(paste0(x$YearMonth, '01'), format = '%Y%m%d') # Formatting as dates
x$Population <- x$Population * 100 # Scaling population to show large numbers
x <- x[with(x, order(YearMonth)), ] # Sorting by date
p <- plot_ly(
  data = x, x = ~YearMonth, y = ~Population, type = 'scatter', mode = 'lines'
) %>% layout(
  xaxis = list(tickformat = '%Y.%m'), # This formatting option should help with your desired format
  yaxis = list(tick0 = min(x$Population), dtick = 5000, tickformat = '.2s') # Be sure to include min for tick0
)
p

这将导致下图- enter image description here