How can I calculate the area inside the contour in R?

I am wondering if it is possible to recoup the area inside the contour in R.

For example, the area of ​​the contour, which is obtained 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 may be confusing. If it's too hard to read. Any example where you can show me how to calculate the area of ​​a simply created contour would be helpful.

Thanks for any help.

+6
source share
2 answers

Thanks to @DWin for the reproducible example and the authors of sos (my favorite 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, this is the same algorithm, but implemented as part of the Fortran program in the splancs package ...)

+5
source

I am going to assume that you are working with an object returned by the Lines contour. (An unnamed list with x and y components at each level.) I expected to find this in a convenient location, but instead found a pdf file that provided an algorithm that I vaguely remember as http: //finzi.psych.upenn. edu / R / library / PBSmapping / doc / PBSmapping-UG.pdf (see pdf p. 19 labeled "-11-") (note added: Wikipedia article on the "training ground" provides this discussion of Surveyors Formula: http: // www.maa.org/pubs/Calc_articles/ma063.pdf , which justifies my use of abs ().)

Building an example:

  x <- 10*1:nrow(volcano) y <- 10*1:ncol(volcano) contour(x, y, volcano); clines <- contourLines(x, y, volcano) x <- clines[[9]][["x"]] y <- clines[[9]][["y"]] level <- clines[[9]][["level"]] level #[1] 130 

Area at the level == 130 (chosen because there are no two 130 levels and does not correspond to any of the boundaries of the graph):

 A = 0.5* abs( sum( x[1:(length(x)-1)]*y[2:length(x)] - y[1:(length(x)-1)]*x[2:length(x)] ) ) A #[1] 233542.1 
+6
source

Source: https://habr.com/ru/post/903229/


All Articles