Question

I want to find the total area from multiple polygons within different contour lines from kernel densities (kde2d). Here is an image of the kernel density and the 50% contour line. How do I calculate the area within the 50% contour line?

I also created a matrix of lat lon coordinates, which represents the points within this 50% contour line. Would it be easier to calculate the total area using these points.

Any suggestions would be greatly appreciated!

Was it helpful?

Solution

Once you have your coordinates in a cartesian system, and have done the kernel smoothing using those coordinates, you can use the contourLines function to get the coordinates of the lines, and then the areapl function from the splancs package to compute the area of each simple ring.

For example, using the example in help(kde2d):

attach(geyser)

plot(duration, waiting, xlim = c(0.5,6), ylim = c(40,100))
f1 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100))
image(f1)
contour(f1)

so that's our data set up - suppose we want the area in the 0.008 contour:

C8 = contourLines(f1,level=0.008)
length(C8)
[1] 3

Now C8 is a list of length 3. We need to apply the areapl function over each of these:

> sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))})
[1] 14.65282 12.27329 14.75005

And we can obviously sum:

> sum(sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))}))
[1] 41.67617

Now this only makes sense if the coordinates are cartesian, and if the contour lines are complete loops. If the 0.008 contour was near the edge then its possible for the contour to get clipped to the bounding box and then bad things happen. Check at least that the last point of each ring is the same as the first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top