r - 将点划分为区域
问题描述
我有一堆点,我想在网格中分配区域。我的点是沿 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
函数,但这很容易搞砸。我觉得应该有一个更简单的方法来做到这一点,但我无法弄清楚。
任何帮助表示赞赏!
解决方案
需要采取 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)
推荐阅读
- dart - 收听 ModalBottomSheet 关闭
- javascript - 如何为这些嵌套的 HTML 列表正确生成数据结构
- python - 使用数据框进行 Python 复杂操作
- elasticsearch - 不了解 _id 在 ES 上的行为
- c++ - Constexpr 替代放置 new 以使内存中的对象未初始化?
- php - php 在 Mac OS 上超时
- octobercms - 在Octobercms item.viewBag.cssClass 值中,具有多个类的 if 语句分别与每个类一起使用
- php - Laravel 计划命令不运行
- javascript - mdl-stepper Stepper.next() 抛出 TypeError
- javascript - 脚本加载发生在 JSDOM 中的窗口加载之后