[在R中工作,我试图绘制河流横截面,在与确定的“倾斜”点相对的交点处插入点,并计算倾斜线下方的面积。它是处理许多横截面的循环的一部分。我想出的最佳解决方案是使用逼近函数,但是所有点都不完全在交点上,因此我无法弄清楚我在做什么错。
很难提供样本数据,因为它是循环的一部分,但是下面的代码样本在图像中产生结果。蓝色三角形应该位于虚线“实心”线和实心横截面周线之间的交点。
###sample data
stn.sub.sort <- data.frame(dist = c(0,1.222,2.213,2.898,4.453,6.990,7.439,7.781,8.753,10.824,10.903,13.601,17.447), depth=c(-0.474,-0.633,0,-0.349,-1.047,-2.982,-2.571,-3.224,-3.100,-3.193,-2.995,-0.065,-0.112), Bankful = c(0,0,0,0,1,0,0,0,0,0,0,0,0))
###plot cross section with identified bankful
plot(stn.sub.sort$dist,
as.numeric(stn.sub.sort$depth),
type="b",
col=ifelse(stn.sub.sort$Bankful==1,"red","black"),
ylab="Depth (m)",
xlab="Station (m)",
ylim=range(stn.sub.sort$depth),
xlim=range(stn.sub.sort$dist),
main="3")
###visualize bankful line of intersection
abline(h=stn.sub.sort$depth[stn.sub.sort$Bankful==1],
lty=2,
col="black")
###approximate point at intersection
index.bf=which(stn.sub.sort$Bankful==1)
index.approx<-which(stn.sub.sort$dist>stn.sub.sort$dist[index.bf])
sbf <- approx(stn.sub.sort$depth[index.approx],
stn.sub.sort$dist[index.approx],
xout=stn.sub.sort$depth[index.bf])
###plot opposite bankful points
points(sbf$y,sbf$x,pch=2,col="blue")
因此您的描述对您将要处理的数据的性质提出了许多问题。我将假设它与您的示例大致相同-从第一个倾斜点开始向下移动,然后再通过与该倾斜点的深度相交的曲线再返回一次。