R使用基本图()绘制的访问线

时间:2017-01-29 12:01:28

标签: r plot graphics timeserieschart

我正在尝试执行以下操作:

  • 使用折线
  • 在R中绘制时间序列
  • 绘制叠加的一条或多条水平线
  • 找到所述线与orizo​​ntal线的交叉点

我到目前为止:

set.seed(34398)
c1 <- as.ts(rbeta(25, 33, 12)) 
p <- plot(c1, type = 'l')
# set thresholds
thresholds <- c(0.7, 0.77)

我找不到访问由R绘制的分段线对象的方法。我真的非常想用基本图形来做这件事,同时意识到可能有一个ggplot2混合可用。有什么想法吗?

abline(h=thresholds, lwd=1, lty=3, col="dark grey")

1 个答案:

答案 0 :(得分:1)

我只会做一个门槛。您可以遍历列表以获取所有这些内容。 首先找到点x,使曲线越过x和x + 1之间的阈值

shift = (c1 - 0.7)
Lower = which(shift[-1]*shift[-length(shift)] < 0)

找到实际的交叉点,找到系列的根 - 0.7并绘制

shiftedF = approxfun(1:length(c1), c1-0.7)
Intersections = sapply(Lower, function(x) { uniroot(shiftedF, x:(x+1))$root })
points(Intersections, rep(0.7, length(Intersections)), pch=16, col="red")

Crossing the threshold