问题描述
我需要找到从曲面到R中特定轮廓的体积.以R帮助文件中的轮廓为例:
I need to find the volume from the surface down to a specific contour in R. Taking the contour example from the R help files:
x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
contour(x,y,volcano)
给出结果图,如何找到从特定轮廓线到表面的体积.
given the resulting graph, how do I find the volume from a specific contour line up to the surface.
在实践中,我将使用bkde2D获得散点图的密度图.由此,我可以绘制等高线图,但我想确定结果图中各种密度临界值所定义的体积.
In practice, I will use bkde2D to get a density map for a scatter plot. From this I can make the contour plot, but I would like to determine the volume defined by various density cutoffs in the resulting plot.
推荐答案
函数 contour
仅绘制轮廓线,但不返回任何值.您需要使用的功能是 contourLines
.
Function contour
just draw the contour lines but doesn't return any values. What you need to use is function contourLines
.
cL <- contourLines(x,y,volcano)
从那里,您可以通过以下方式计算每条轮廓线的面积:
From there, you can calculate the area of each contour lines the following way:
area <- rep(0,length(cL))
for(i in 1:length(cL)){
d <- data.frame(cL[[i]]$x,cL[[i]]$y)
sa <- sb <- 0
for(j in 1:(nrow(d)-1)){
sa <- sa+d[j,1]*d[j+1,2]
sb <- sb+d[j,2]*d[j+1,1]
}
area[i] <- abs((sa-sb)/2)
}
area
[1] 1.413924e+05 3.109685e+04 2.431528e+04 2.049473e+04 6.705976e+04 3.202145e+05 1.720469e+03
[8] 2.926802e+05 2.335421e+05 1.834791e+05 1.326162e+05 4.672784e+02 9.419792e+04 5.121851e+03
[15] 5.126860e+04 3.660862e-01 1.216750e+03 2.051307e+04 4.670745e+02 4.146927e+03
现在,如果要在两条轮廓线之间(例如,在120级和130级之间)设置音量:
Now, if you want the volume between two contour lines (say between levels 120 and 130):
level1 <- 120
level2 <- 130
levels <- unlist(lapply(cL,function(x)x$level))
base <- (1:length(cL))[level==level1]
top <- (1:length(cL))[level==level2]
vol <- (level[top]-level[base])*(area[base]+area[top])/2
vol
[1] 2631111
这是我能做到的,因为如果将下一个轮廓线分成几个扇区,我看不到如何进行.
And that's as far as I can go because I don't see how to proceed if the next contour line is split into several sectors.
这篇关于从表面到R轮廓的体积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!