开发者

Adding vertical lines to a plot.ts() graph in R

I would like to add a vertical line to a plot.ts() graph:

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)

I tried abline(v=8.75) but this did not put the line where I hoped. Since I have two columns of graphs in the graphics window with this function, I need to add two vertical lines, one for each column of开发者_C百科 graphs. Any ideas?


You need to create a panel function that will operate within each of the panels that ts.plot creates to handle multiple series. It needs to duplicate how the lines() function handles arguments as well as accept an argument for abline that will then "work" in the local coordinate system:

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

It's like augmenting lattice functions, except it is all done in base graphics.

It might be better to leave off the setting of vpos in the argument list. Then you will have a handle on it from the outside and won't need to rewrite the function. (Your choice. I was getting warnings from the "graphical police" when I tried passing it in the argument list of 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜