Search code examples
rareacontour

How can I calculate the area within a contour in R?


I'm wondering if it is possible to caclulate the area within a contour in R.

For example, the area of the contour that results from:

sw<-loess(m~l+d)
mypredict<-predict(sw, fitdata) # Where fitdata is a data.frame of an x and y matrix

contour(x=seq(from=-2, to=2, length=30), y=seq(from=0, to=5, length=30), z=mypredict)

Sorry, I know this code might be convoluted. If it's too tough to read. Any example where you can show me how to calculate the area of a simply generated contour would be helpful.

Thanks for any help.


Solution

  • Thanks to @DWin for reproducible example, and to the authors of sos (my favourite R package!) and splancs ...

    library(sos)
    findFn("area polygon compute")
    library(splancs)
    with(clines[[9]],areapl(cbind(x,y)))
    

    Gets the same answer as @DWin, which is comforting. (Presumably it's the same algorithm, but implemented within a Fortran routine in the splancs package ...)