R - How to find points within specific Contour

前端 未结 2 1591
灰色年华
灰色年华 2020-12-05 19:33

I am creating density plots with kde2d (MASS) on lat and lon data. I would like to know which points from the original data are within a specific contour.

I create

2条回答
  •  不思量自难忘°
    2020-12-05 20:06

    You can use point.in.polygon from sp

    ## Interactively check points
    plot(bw)
    identify(bw$lon, bw$lat, labels=paste("(", round(bw$lon,2), ",", round(bw$lat,2), ")"))
    
    ## Points within polygons
    library(sp)
    dens <- kde2d(x, y, n=200, lims=c(c(-73, -70), c(-13, -12)))  # don't clip the contour
    ls <- contourLines(dens, level=levels)
    inner <- point.in.polygon(bw$lon, bw$lat, ls[[2]]$x, ls[[2]]$y)
    out <- point.in.polygon(bw$lon, bw$lat, ls[[1]]$x, ls[[1]]$y)
    
    ## Plot
    bw$region <- factor(inner + out)
    plot(lat ~ lon, col=region, data=bw, pch=15)
    contour(dens, levels=levels, labels=prob, add=T)
    

    enter image description here

提交回复
热议问题