如何在R中的时间序列图的点旁边增加值?

时间:2018-07-13 14:51:35

标签: r

我正在尝试将预测的时间序列的值添加到r中的绘图中。

我有

par(mfrow = c(1,1))

plot(train,type='l',xlim=c(2017,2019.5),ylim=c(75,200),xlab = 'Year',ylab = 'Sales')

title(main="13 Month Sales Forecast")

lines(e, col='green')

points(train, pch=19)

points(e,col='green' ,pch=19)

其中e是由我想要的值组成的时间序列,train是我用于生成模型的数据。

上面的当前代码生成了我想要的图形,除了我想按点显示时间序列预测e的值。

我尝试了text(e),将数字放在点上,但是数字是1,2,3,...,依此类推,不是时间序列e所包含的预测值。 / p>

1 个答案:

答案 0 :(得分:0)

您应该在label中明确添加参数text,如下所示:

 # Train data.frame simulation
set.seed(123)
year = seq(2017, 2019.5, length.out = 20)
Sales = seq(75, 200, length.out = 20)
train <- data.frame(year, Sales)

# Source data simulation
year = seq(2017, 2019.5, length.out = 10)
Sales_source = seq(75, 200, length.out = 10) + rnorm(10) * 10
e <- data.frame(year, Sales_source)


# plotting
par(mfrow = c(1,1))

plot(train, type="l", xlim = c(2017, 2019.5), 
     ylim = c(75, 200), xlab = "Year", ylab = "Sales")

title(main = "13 Month Sales Forecast")
lines(e, col = "green")
points(train, pch = 19)
points(e, col = "green", pch = 19)

# Adding labels
text(e, labels = ceiling(Sales_source), pos = 2)

输出: forecast