R - 如何在特定轮廓内找到点 [英] R - How to find points within specific Contour

查看:133
本文介绍了R - 如何在特定轮廓内找到点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用kde2d(MASS)在纬度和经度数据上创建密度图。我想知道原始数据中的哪些点位于特定轮廓内。

我使用两种方法创建90%和50%轮廓。我想知道哪些点在90%轮廓内,哪些点在50%轮廓内。 90%轮廓中的点将包含50%轮廓内的所有点。最后一步是找到90%轮廓内不在50%轮廓内的点(我不一定需要此步骤的帮助)。

 #bw = 2 cols(lat和lon)和363 rows 
#两个版本的数据:
#将理想地使用第二个版本(使用ggplot2)

#版本1(不使用ggplot2)
库(MASS)
x < - bw $ lon
y< -bw $ lat
dens< -kde2d(x,y,n = 200)

#绘制等高线
prob< -c (0.9,0.5)
dx <-diff(dens $ x [1:2])
dy < - diff(dens $ y [1:2])
sz< - 分类(密码$ z)
c1 < - cumsum(sz)* dx * dy
levels < - sapply(prob,function(x){
approx(c1,sz, (x,y)
轮廓(dens,levels = levels,labels = prob,add = T)


$) code>

这里是第2版 - 使用ggplot2。理想情况下,我希望使用此版本查找90%和50%轮廓内的点。

 #version 2(with ggplot2)
getLevel< - function(x,y,prob){
kk < - MASS :: kde2d(x,y)
dx < - diff(kk $ x [1:2])
dy < - diff(kk $ y [1: 2])
sz< - sort(kk $ z)
c1< - cumsum(sz)* dx * dy
approx(c1,sz,xout = 1 - prob)$ y
}

#90和50%等值线
L90 < - getLevel(bw $ lon,bw $ lat,0.9)
L50 < - getLevel( bw $ lon,bw $ lat,0.5)

kk < - MASS :: kde2d(bw $ lon,bw $ lat)
dimnames(kk $ z)< - list kk $ x,kk $ y)
dc < - 熔化(kk $ z)

p < - ggplot(dc,aes(x = Var1,y = Var2))+ geom_tile (aes(fill = value))
+ geom_contour(aes(z = value),breaks = L90,color =red)
+ geom_contour(aes(z = value),breaks = L50,颜色=黄色)
+ ggtitle(BW的90(红色)和50(黄色)轮廓)

我绘制了绘制所有纬度和经度点以及90%和50%等高线的图。我只想知道如何提取90%和50%轮廓线内的精确点。



我试图找到z值(来自kde2d的密度图)与每行lat和lon值相关,但没有运气。我也在想,我可以在数据中添加一个ID列来标记每行,然后在使用 melt()后以某种方式传输。然后,我可以简单地对具有与我想要的每个轮廓相匹配的z值的数据进行子集化,并根据ID列来查看它们与原始BW数据进行比较的经纬度和纬度。

以下是我正在谈论的内容:

注意:这些代码大部分来自其他问题。大声喊出所有捐助者!



谢谢!


$ ##交互检查点
plot(bw)
identify(bw $ lon,bw $ lat,labels = paste((,round(bw $ lon,2),,,round(bw $ lat,2),)))

##多边形中的点
库(sp)
dens <-kde2d(x,y,n = 200,lims = c(c(-73,-70),c (-13,-12)))#不剪切轮廓
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)

##地块
bw $地区< - 因子(内+出)
图(经纬度, =区域,data = bw,pch = 15)
轮廓(dens,levels = levels,labels = prob,add = T)


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 90% and 50% contours using two approaches. I want to know which points are within the 90% contour and which points are within the 50% contour. The points in the 90% contour will contain all of those within the 50% contour. The final step is to find the points within the 90% contour that are not within the 50% contour (I do not necessarily need help with this step).

# bw = data of 2 cols (lat and lon) and 363 rows
# two versions to do this: 
# would ideally like to use the second version (with ggplot2)

# version 1 (without ggplot2) 
library(MASS)
x <- bw$lon
y <- bw$lat
dens <- kde2d(x, y, n=200)

# the contours to plot
prob <- c(0.9, 0.5)
dx <- diff(dens$x[1:2])
dy <- diff(dens$y[1:2])
sz <- sort(dens$z)
c1 <- cumsum(sz) * dx * dy 
levels <- sapply(prob, function(x) { 
    approx(c1, sz, xout = 1 - x)$y
})
plot(x,y)
contour(dens, levels=levels, labels=prob, add=T)

And here is version 2 - using ggplot2. I would ideally like to use this version to find the points within the 90% and 50% contours.

# version 2 (with ggplot2)
getLevel <- function(x,y,prob) { 
    kk <- MASS::kde2d(x,y)
    dx <- diff(kk$x[1:2])
    dy <- diff(kk$y[1:2])
    sz <- sort(kk$z)
    c1 <- cumsum(sz) * dx * dy
    approx(c1, sz, xout = 1 - prob)$y
}

# 90 and 50% contours
L90 <- getLevel(bw$lon, bw$lat, 0.9)
L50 <- getLevel(bw$lon, bw$lat, 0.5)

kk <- MASS::kde2d(bw$lon, bw$lat)
dimnames(kk$z) <- list(kk$x, kk$y)
dc <- melt(kk$z)

p <- ggplot(dc, aes(x=Var1, y=Var2)) + geom_tile(aes(fill=value)) 
+ geom_contour(aes(z=value), breaks=L90, colour="red")
+ geom_contour(aes(z=value), breaks=L50, color="yellow")
+ ggtitle("90 (red) and 50 (yellow) contours of BW")

I create the plots with all of the lat and lon points plotted and 90% and 50% contours. I simply want to know how to extract the exact points that are within the 90% and 50% contours.

I have tried to find the z values (the elevation of the density plots from kde2d) that are associated with each row of lat and lon values but had no luck. I was also thinking I could add an ID column to the data to label each row and then somehow transfer that over after using melt(). Then I could simply subset the data that has values of z that match each contour I want and see which lat and lon they are compared to the original BW data based on the ID column.

Here is a picture of what I am talking about:

I want to know which red points are within the 50% contour (blue) and which are within the 90% contour (red).

Note: much of this code is from other questions. Big shout-out to all those who contributed!

Thank you!

解决方案

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)

这篇关于R - 如何在特定轮廓内找到点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆