【问题标题】:Drawing a line on time series plot在时间序列图上画一条线
【发布时间】:2017-12-02 09:53:43
【问题描述】:

我有下一个时间序列对象,我使用 plot 绘制它:

ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
plot(ts)

Plot of the ts

现在,我想使用 abline 在第一次和最后一次观察之间画一条线,但是,虽然 R 没有显示任何错误,但在时间序列图上使用它似乎不起作用.用来画线的代码是:

abline(a = 1, b = (ts[length(ts)]- ts[1]) / (length(ts)-1))

有人遇到过同样的问题并设法解决了吗?

【问题讨论】:

  • 完全正确,我将修复该错误。谢谢!

标签: r plot time-series line


【解决方案1】:
ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
plot(ts, xlim=c(1,8), type="o")

x1 <- 1; y1 <- ts[1]
x2<- 7.75; y2 <- ts[length(ts)]

abline(a = y1-x1*(y1-y2)/(x1-x2), b = (y1-y2)/(x1-x2) )
grid()

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解您想要实现的目标。但以下内容可能会有所帮助。

    您可以通过lines 使用以下简单代码:

    # your code
    ts <- ts(c(1:4,2:5,3:6,4:7,5:8,6:9,7:10), frequency = 4)
    plot(ts)
    
    # drawing a blue line from (1, 1) to (8, 10)
    lines(x = c(1, 8), y = c(1,10), col="blue")
    

    这会产生以下简单的情节

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2018-04-02
      相关资源
      最近更新 更多