【问题标题】:Plotly Time series forecasting in R - modify default x axis and y axis rangeR中的Plotly时间序列预测 - 修改默认x轴和y轴范围
【发布时间】:2018-07-01 16:49:22
【问题描述】:

我有一个关于使用 plotly 进行时间序列预测的查询。我已经建立了一个预测图表,但是几乎没有什么需要改变的,但它对我不起作用。详情请看附件,R代码的更正或建议请告诉我。

查询:

  1. 目前 X 轴显示 2,016.5, 2017 , 2017.5 等,但我希望它显示 yearmonth 像 2016.04,2014,05 等。 请注意年月是数据中的字段,请参考附件数据

  2. 目前 Y 轴显示的标签有 50 K 的差异,但我希望它显示为 5 K、10K、15K 等。

以下是使用的 R 代码:

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")

以下是示例数据:

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

【问题讨论】:

    标签: r plotly forecasting


    【解决方案1】:

    您希望将日期格式设置为Rdates。这将有助于格式化。

    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
    

    结果如下图 -

    【讨论】:

    • 很高兴为您提供帮助!如果您认为它解决了您的问题,您会考虑投票和/或接受我的回答吗?
    • Ameya 我将其标记为正确答案。需要更多信息。我原来的帖子也有预测代码。如何在此重用预测代码。
    • 您需要将time(ts) 转换为R date 格式。这在基础R 中很棘手。您可以尝试zoo::as.Date.yearmonts 对象中提取时间。
    【解决方案2】:

    在 plotly 中自定义轴的最佳方法是使用您想要的选项设置一个变量。

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

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

    这只会在 x 轴上每隔 1 间隔显示刻度。我意识到这不是您要问的。使用 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 代码中。

    希望这对您有所帮助。

    【讨论】:

    • 您好,M. Wickers,感谢您的回复。 Y 轴按预期完美运行。但是,XAxis 不工作。在您的代码显示 2017 和 2018 之后。我需要按照我的 YearMonth Feild 显示。请从我的问题中找到附件。我的数据从 2017.01 到 2018.04 开始,我需要 XAxis 是 2017.01、2017.02...等....直到 2018.04。请提供您的建议。
    • 抱歉耽搁了。我还没有找到一个明确的方法来做到这一点。对我来说,它使 x 轴不可读。这就是为什么我建议只增加 1 年。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2011-11-06
    相关资源
    最近更新 更多