volume from the surface down to a contour in R

核能气质少年 提交于 2021-02-10 18:12:19

问题


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.

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/14515291/volume-from-the-surface-down-to-a-contour-in-r

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