使用基本 plot() 访问 R 绘制的线
Access lines plotted by R using basic plot()
我正在尝试执行以下操作:
- 使用折线在 R 中绘制时间序列
- 绘制一条或多条叠加的水平线
- 找到所述线与水平线的交点
我走到这一步:
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")
我只会做一个阈值。您可以遍历列表以获取所有这些。
首先找到点 x,使曲线穿过 x 和 x+1
之间的阈值
shift = (c1 - 0.7)
Lower = which(shift[-1]*shift[-length(shift)] < 0)
通过找到 Series - 0.7 的根和 plot
找到实际的交叉点
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")
我正在尝试执行以下操作:
- 使用折线在 R 中绘制时间序列
- 绘制一条或多条叠加的水平线
- 找到所述线与水平线的交点
我走到这一步:
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")
我只会做一个阈值。您可以遍历列表以获取所有这些。 首先找到点 x,使曲线穿过 x 和 x+1
之间的阈值shift = (c1 - 0.7)
Lower = which(shift[-1]*shift[-length(shift)] < 0)
通过找到 Series - 0.7 的根和 plot
找到实际的交叉点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")