我将谈论您最初的火山类型图问题,而不是虚构的问题,因为它们完全不同。
所以我真的想了很多,我相信我得出了一个可靠的结论。有两种选择:
1. 你知道直线的方程,这很容易处理。
2. 你不知道直线的方程,这意味着我们需要使用近似值。
一些几何图形:
函数 显示直线方程。对于给定的坐标对 (x, y),如果 y > 当您传入 x 时等式的右侧,则该点位于直线上方,否则位于直线下方。如果您有曲线(如您的情况),则同样的概念也成立。
如果您有方程式,那么很容易在下面的代码中执行上述操作,并且您已设置好。如果不是,您需要对曲线进行近似。为此,您将需要以下代码:
df=data.frame(x=seq(2,16,by=2),y=seq(2,16,by=2),lab=paste("label",seq(2,16,by=2),sep=''))
make_vector <- function(df) {
lab <- vector()
for (i in 1:nrow(df)) {
this_row <- df[i,] #this will contain the three elements per row
if ( (this_row[1] < max(line1x) & this_row[2] > max(line1y) & this_row[2] < a + b*this_row[1])
|
(this_row[1] > min(line2x) & this_row[2] > max(line2y) & this_row[2] > a + b*this_row[1]) ) {
lab[i] <- this_row[3]
} else {
lab[i] <- <NA>
}
}
return(lab)
}
#this_row[1] = your x
#this_row[2] = your y
#this_row[3] = your label
df$labels <- make_vector(df)
plot(df[,1],df[,2])
# adding lines
lines(seq(1,15),seq(15,1),lwd=1, lty=2)
# adding labels
text(df[,1],df[,2],labels=df[,4],pos=3,col="red",cex=0.75)
重要的是功能。想象一下,当你用 x,y 和 labs 创建它时,你有 df。您还将有一个向量,其中 line1 的 x,y 坐标和 line2 的 x,y 坐标。
我们只看line1的情况(上面代码实现的line 2也是一样的):
this_row[1] < max(line1x) & this_row[2] > max(line1y) & this_row[2] < a + b*this_row[1]
#translates to:
#this_row[1] < max(line1x) = your x needs to be less than the max x (vertical line in graph below
#this_row[2] > max(line1y) = your y needs to be greater than the max y (horizontal line in graph below
#this_row[2] < a + b*this_row[1] = your y needs to be less than the right hand side of the equation (to have a point above i.e. left of the line)
#check below what the line is
这将产生类似于下图的效果(这有点可怕,也被放大了,但它只是一个参考。可视化它接近你的线条):
上面的代码会选择三角形上方区域和 y=1 和 x=1 线内的所有点。
最后等式:
拥有 2 个点的坐标,您可以计算出一条线的方程,求解一个由两个方程和 2 个参数 a 和 b 组成的系统。 (y = a +bx 通过替换每个点的 y,x)
要选取的 2 个点是最接近第一条线 (line1) 的切线的两个点。根据您的数据任意选择。越接近切线越好。只需绘制斑点和眼球。
完成上述所有操作后,您的标签就有了您的观点(至少大约如此)。
这是你唯一能做的!
长篇大论,但希望对您有所帮助。
附:我没有测试代码,因为我没有数据。