向R中的plot.ts()图添加垂直线

Jot*_*ota 4 plot r time-series

我想在plot.ts()图表上添加一条垂直线:

plot.ts(cbind(a, b, c, d, e, f, g, h),main="Time Series")
a<-seq(1:16);
b<-seq(1:16);
c<-seq(1:16);
d<-seq(1:16);
e<-seq(1:16);
f<-seq(1:16);
g<-seq(1:16);
h<-seq(1:16)
Run Code Online (Sandbox Code Playgroud)

我尝试过,abline(v=8.75)但这并没有达到我希望的目标。由于在具有此功能的图形窗口中有两列图形,因此我需要添加两条垂直线,每条垂直线一条。有任何想法吗?

42-*_*42- 5

您需要创建一个面板功能,该功能将在ts.plot创建的每个面板中运行以处理多个系列。它需要复制lines()函数如何处理参数以及接受用于abline的参数,然后该参数将在局部坐标系中“工作”:

?ts.plot
my.ts.panel <- function(x, col = col, bg = bg, pch = pch, type = type,  vpos=8.75, ...){
      lines(x, col = col, bg = bg, pch = pch, type = type, ...)
      abline(v=vpos)}
plot.ts(cbind(a, b, c, d, e, f, g, h),main="Time Series", panel=my.ts.panel)
Run Code Online (Sandbox Code Playgroud)

就像增加晶格功能一样,只是它们全部在基础图形中完成。

最好在参数列表中保留vpos的设置。然后,您将可以从外部对其进行操作,而无需重写该函数。(您的选择。当我尝试在ts.plot的参数列表中传递它时,我从“图形警察”那里得到警告。):

vpos=8.75
my.ts.panel <- function(x, col = col, bg = bg, pch = pch, type = type,   ...){
      lines(x, col = col, bg = bg, pch = pch, type = type, ...)
      abline(v=vpos)}
plot.ts(cbind(a, b, c, d, e, f, g, h),main="Time Series", panel=my.ts.panel)
Run Code Online (Sandbox Code Playgroud)