将点划分为区域

Dividing points into zones

我有一堆要在网格中分配区域的点。我的点是从 -100:100 沿 x 轴和 -42.5:42.5 沿 y 轴。我想创建一个整体 10x7 的网格,这意味着各个框为 20x12.143。下面是数据的重新前缀示例,网格线表示我希望如何划分数据。

x <- seq(-100, 100, length.out = 50)
y <- seq(-42.5, 42.5, length.out = 50)
points <- merge(x, y)

points %>%
  ggplot() +
  geom_point(aes(x, y), color = "lightblue")  +
  theme_minimal() +
  #start of grid points
  geom_segment(aes(x = -100, xend = -100, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -80, xend = -80, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -60, xend = -60, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -40, xend = -40, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -20, xend = -20, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 0, xend = 0, y = -42.5, yend = 42.5))  +
  geom_segment(aes(x = 80, xend = 80, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 60, xend = 60, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 40, xend = 40, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 20, xend = 20, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 100, xend = 100, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -100, xend = 100, y = -30.357, yend = -30.357)) +
  geom_segment(aes(x = -100, xend = 100, y = -18.214, yend = -18.214)) +
  geom_segment(aes(x = -100, xend = 100, y = -6.071, yend = -6.071)) +
  geom_segment(aes(x = -100, xend = 100, y = 30.357, yend = 30.357)) +
  geom_segment(aes(x = -100, xend = 100, y = 18.214, yend = 18.214)) +
  geom_segment(aes(x = -100, xend = 100, y = 6.071, yend = 6.071)) +
  geom_segment(aes(x = -100, xend = 100, y = -42.5, yend = -42.5)) +
  geom_segment(aes(x = -100, xend = 100, y = 42.5, yend = 42.5))

我想做的是为每个区域中的每个点分配一个唯一的区域 ID(例如区域 1 到区域 70)。我可能可以编写一个庞大的 ifelse 函数,但这很容易搞砸。我觉得应该有更简单的方法来做到这一点,但我想不通。

感谢任何帮助!

需要执行 4 个步骤,首先为网格定义沿 x 轴和 y 轴的两个序列。其次,您制作了一个矩阵,可以通过 x 和 y 序列的 seq_along() 和 ID returns 进行索引。

xbreaks <- seq(-100, 100, length.out = 10) 
ybreaks <- seq(-42.5, 42.5, length.out = 8)

id <- matrix(seq_len(length(xbreaks) * length(ybreaks)),
             length(xbreaks), length(ybreaks))

随后我们可以使用findInterval()将点匹配到网格中x和y方向的位置。然后可以使用此位置来索引上面定义的 id 矩阵。

# Make points
x <- seq(-100, 100, length.out = 50)
y <- seq(-42.5, 42.5, length.out = 50)
points <- merge(x, y)

# Match points to grid location
xi <- findInterval(points$x, xbreaks)
yi <- findInterval(points$y, ybreaks)

# Subset with 2-column matrix
points$id <- id[cbind(xi, yi)]

这就是 ID 的样子。

library(ggplot2)

ggplot(points) +
  geom_point(aes(x, y, colour = as.factor(id)))  +
  theme_minimal() +
  #start of grid points
  geom_segment(aes(x = -100, xend = -100, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -80, xend = -80, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -60, xend = -60, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -40, xend = -40, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -20, xend = -20, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 0, xend = 0, y = -42.5, yend = 42.5))  +
  geom_segment(aes(x = 80, xend = 80, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 60, xend = 60, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 40, xend = 40, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 20, xend = 20, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = 100, xend = 100, y = -42.5, yend = 42.5)) +
  geom_segment(aes(x = -100, xend = 100, y = -30.357, yend = -30.357)) +
  geom_segment(aes(x = -100, xend = 100, y = -18.214, yend = -18.214)) +
  geom_segment(aes(x = -100, xend = 100, y = -6.071, yend = -6.071)) +
  geom_segment(aes(x = -100, xend = 100, y = 30.357, yend = 30.357)) +
  geom_segment(aes(x = -100, xend = 100, y = 18.214, yend = 18.214)) +
  geom_segment(aes(x = -100, xend = 100, y = 6.071, yend = 6.071)) +
  geom_segment(aes(x = -100, xend = 100, y = -42.5, yend = -42.5)) +
  geom_segment(aes(x = -100, xend = 100, y = 42.5, yend = 42.5))

reprex package (v1.0.0)

创建于 2021-02-19