R - locate intersection of two curves

假如想象 提交于 2020-01-30 03:16:36

问题


There are a number of questions in this forum on locating intersections between a fitted model and some raw data. However, in my case, I am in an early stage project where I am still evaluating data.

To begin with, I have created a data frame that contains a ratio value whose ideal value should be 1.0. I have plotted the data frame and also used abline() function to plot a horizontal line at y=1.0. This horizontal line and the plot of ratios intersect at some point.

plot(a$TIME.STAMP, a$PROCESS.RATIO,
     xlab='Time (5s)',
     ylab='Process ratio',
     col='darkolivegreen',
     type='l')
abline(h=1.0,col='red')

My aim is to locate the intersection point, say x and draw two vertical lines at x±k, as abline(v=x-k) and abline(v=x+k) where, k is certain band of tolerance.

Applying a grid on the plot is not really an option because this plot will be a part of a multi-panel plot. And, because ratio data is very tightly laid out, the plot will not be too readable. Finally, the x±k will be quite valuable in my discussions with the domain experts.

Can you please guide me how to achieve this?


回答1:


Here are two solutions. The first one uses locator() and will be useful if you do not have too many charts to produce:

x <- 1:5
y <- log(1:5)
df1 <-data.frame(x= 1:5,y=log(1:5))
k <-0.5

plot(df1,type="o",lwd=2)
abline(h=1, col="red")
locator()

By clicking on the intersection (and stopping the locator top left of the chart), you will get the intersection:

> locator()
$x
[1] 2.765327
$y
[1] 1.002495

You would then add abline(v=2.765327).

If you need a more programmable way of finding the intersection, we will have to estimate the function of your data. Unfortunately, you haven’t provided us with PROCESS.RATIO, so we can only guess what your data looks like. Hopefully, the data is smooth. Here’s a solution that should work with nonlinear data. As you can see in the previous chart, all R does is draw a line between the dots. So, we have to fit a curve in there. Here I’m fitting the data with a polynomial of order 2. If your data is less linear, you can try increasing the order (2 here). If your data is linear, use a simple lm.

fit <-lm(y~poly(x,2))
newx <-data.frame(x=seq(0,5,0.01))
fitline = predict(fit, newdata=newx)

est <-data.frame(newx,fitline)

plot(df1,type="o",lwd=2)
abline(h=1, col="red")
lines(est, col="blue",lwd=2)

Using this fitted curve, we can then find the closest point to y=1. Once we have that point, we can draw vertical lines at the intersection and at +/-k.

cross <-est[which.min(abs(1-est$fitline)),] #find closest to 1

plot(df1,type="o",lwd=2)
abline(h=1)
abline(v=cross[1], col="green")
abline(v=cross[1]-k, col="purple")
abline(v=cross[1]+k, col="purple")



来源:https://stackoverflow.com/questions/34248347/r-locate-intersection-of-two-curves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!