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

发布于 2024-11-05 19:51:32 字数 369 浏览 2 评论 0原文

我想在 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)

我尝试了 abline(v=8.75) 但这并没有将线放在我希望的位置。由于我使用此函数在图形窗口中有两列图形,因此我需要添加两条垂直线,每列图形各一条。有什么想法吗?

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 graphs. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-11-12 19:51:32

您需要创建一个面板函数,该函数将在 ts.plot 创建的每个面板中运行以处理多个系列。它需要复制lines()函数处理参数的方式,并接受abline的参数,然后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)

这就像增强晶格函数,只不过它都是在基础图形中完成的。

最好不要在参数列表中设置 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文