R 中的 Plotly 时间序列预测 - 修改默认的 x 轴和 y 轴范围
Plotly Time series forecasting in R - modify default x axis and y axis range
我对使用 plotly 进行时间序列预测有疑问。我已经建立了一个预测图表,但几乎不需要更改,但它对我不起作用。详情请见附件,如有R代码的更正或建议,请告知。
查询:
当前 X 轴显示 2,016.5, 2017, 2017.5 等,但我希望它显示 yearmonth,如 2016.04,2014,05 等。
请注意yearmonth是数据中的一个字段,请参考附件数据
目前 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
在 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 代码中。
希望这对您有所帮助。
您想将日期格式化为 R
date
s。这将有助于格式化。
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
这导致下图 -
我对使用 plotly 进行时间序列预测有疑问。我已经建立了一个预测图表,但几乎不需要更改,但它对我不起作用。详情请见附件,如有R代码的更正或建议,请告知。
查询:
当前 X 轴显示 2,016.5, 2017, 2017.5 等,但我希望它显示 yearmonth,如 2016.04,2014,05 等。 请注意yearmonth是数据中的一个字段,请参考附件数据
目前 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
在 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 代码中。
希望这对您有所帮助。
您想将日期格式化为 R
date
s。这将有助于格式化。
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
这导致下图 -