【发布时间】:2020-05-24 21:30:42
【问题描述】:
我正在运行一个 QAQC 协议,并且有一个带有 x 和 y 坐标的点和一个多边形的数据集,并且想要确定这些点是否落入多边形(多边形外的点应该被标记为错误值)。
从绘图的视觉印象来看,一个点在多边形之外,而所有其他值都在多边形之内。然而,point.in.polygon 给出了输出,即多边形内没有点。
我认为我的问题必须与 ggplot 或 geom_build 中的点有关,因为我用我的多边形和随机点值广泛尝试了 point.in.polygon 函数。
这是一个可复制的示例,希望有人能指出我到底在哪里犯了错误。
多边形的值:
mean_aug_temp_ref=13.8
mean_aug_sd_ref=3.906242
sd_aug_temp_ref=3.083504
sd_aug_sd_ref=1.699699
#and these are my point values
data=data.frame("x"=c(16.17419355,16.79354839,16.37096774,15.7483871, 17.07741935,16.18387097,
16.38064516,15.91612903,15,14.42580645,14.91935484,15.5,15.78709677,15.88709677,
23.9,18.22258065,15.51612903,14.8516129,14.93548387,15.93225806,17.6483871,16.57741935,
16.27419355,15.79354839,15.70322581,15.23548387,15.8516129,16.95483871,16.58064516,
16.25806452,18.13225806,16.46774194,16.10645161,14.80322581,16.85806452,13.24516129,
14.28387097,14.56451613),"y"=c(3.422182138,3.325302421,5.216263575,4.932097849,3.247799051,3.658370522,
3.498499886,3.901150792,4.236607552,3.960090498,3.781208758,3.783591385,
3.693390973,3.806386412,0.48730997,2.301078,3.721169197,4.045304928,3.684483053,
3.41859195,2.901957554,3.415018251,3.466360853,3.79302042,3.739892688,4.178312743,
4.041067269,2.901698087,2.832576457,3.230205585,3.063566527,3.068009,3.13238139,
4.655432875,3.282535421,4.515352932,3.374136237,4.564639348))
test_object=ggplot(data=data, aes(x, y))+
geom_point()+ #the point layer
#ellipse for 5 times the sd for mean and sd of reference values
geom_ellipse(aes(a=sd_aug_sd_ref*5, x0=mean_aug_temp_ref, b=sd_aug_temp_ref*5, y0=mean_aug_sd_ref, angle=0))
built <- ggplot_build(test_object)$data
points <- built[[1]] #first list element are the points
ell <- built[[2]] #second list element is the ellipse
dat <- data.frame(
data[,1:2], #first two columns are the coordinates
in.ell = as.logical(point.in.polygon(point.x=points$x, point.y=points$y, pol.x=ell$x, pol.y=ell$y)))
【问题讨论】:
-
您似乎错过了
mean_aug_temp_ref,因此您的代码不可重现。我猜它与 mean(data$x) 相同,但如果是这样,那么所有点都在我绘图的椭圆内。 -
抱歉,我添加了 mean_aug_temp_ref。它与 mean(data$x) 不同,因为它是参考数据集的平均值。
标签: r ggplot2 sp point-in-polygon