无法将事件行添加到 xts 时间序列(R 找不到 "addEventLines" 函数)

Unable to add event line to xts time series (R cannot find "addEventLines" function)

我正在尝试根据此示例中可用的代码使用 R 中的 xts 将事件行添加到时间序列:http://joshuaulrich.github.io/xts/plotting_basics.html

library(xts)
data(sample_matrix) 
sample.xts <- as.xts(sample_matrix)


plot(sample.xts[,4])
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"))
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"), 
              offset=1.2, pos=2, , srt=90, cex=1.5)
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"), 
              offset=1.2, pos=4, , srt=-90, cex=1.5)

但不断收到以下错误消息:

"Error: could not find function "addEventLines" "

有谁知道为什么会发生这种情况,或者可以建议一种将事件线添加到时间序列数据的替代方法?

非常感谢。

addEventLines 功能似乎在 xts 的最新版本中不可用。 使用软件包 devtools,您可以从 github 下载以下版本的 xts,其中该功能可用:

devtools::install_github("R-Finance/xts")

您的代码生成下图:

library(xts)
data(sample_matrix) 
sample.xts <- as.xts(sample_matrix)
plot(sample.xts[,4])
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"))
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"), 
              offset=1.2, pos=2, , srt=90, cex=1.5)
addEventLines(c("2007-03-20","2007-05-28"), c("foo", "bar"), 
              offset=1.2, pos=4, , srt=-90, cex=1.5)