首页 > 解决方案 > 将点划分为区域

问题描述

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

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函数,但这很容易搞砸。我觉得应该有一个更简单的方法来做到这一点,但我无法弄清楚。

任何帮助表示赞赏!

标签: rggplot2scatter-plot

解决方案


需要采取 4 个步骤,首先您为网格定义沿 x 和 y 轴的两个序列。其次,您创建一个矩阵,该矩阵可以由seq_along()x 和 y 序列中的 索引并返回一个 ID。

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 包于 2021-02-19 创建(v1.0.0)


推荐阅读