【问题标题】:R point in polygon analysis from ggplot object来自ggplot对象的多边形分析中的R点
【发布时间】: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


【解决方案1】:

问题是因为这行:

ell <- built[[2]]

如果您检查这个数据框,您会发现它实际上有 38 个椭圆副本(每个点一个)。这是geom_ellipse 是如何创建的人工制品。所以你的“椭圆”实际上是一个 38 周期的循环。解决方案是过滤掉,所以你只得到一个椭圆的副本:

ell <- built[[2]][built[[2]]$group == built[[2]]$group[1],] 

dat <- data.frame(
        data[,1:2], #first two columns are the coordinates
        in.ell = as.logical(point.in.polygon(points$x, points$y, pol.x=ell$x, pol.y=ell$y)))

dat$in.ell
#>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [13]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [37]  TRUE  TRUE

除了第 15 个元素之外,您可以看到所有元素都在椭圆内。

【讨论】:

  • 你真是个天才,非常感谢!你绝对让我又度过了一个不眠之夜!
猜你喜欢
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2019-03-24
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多