r - 带有sf的R中质心外的多边形
问题描述
我有一组质心,其 XY 坐标与不同的组相关联。我想使用sf
.
这是一个可重现的示例:
library(ggplot2)
library(cowplot)
library(dplyr)
library(sf)
ex <- data.frame(rect = c(rep("A", 8), rep("B", 6)),
x = c(1.5, 2.5, 1.5, 2.5, 3.5, 1.5, 2.5, 3.5,
5.5, 5.5, 6.5, 6.5, 7.5, 7.5),
y = c(5.5, 5.5, 4.5, 4.5, 4.5, 3.5, 3.5, 3.5,
1.5, 2.5, 2.5, 3.5, 2.5, 3.5))
# Plot
plot_grid(
ggplot(ex, aes(x, y)) +
geom_tile(color = "black", aes(fill = as.factor(rect)), show.legend = FALSE) +
geom_point() +
labs(title = "ggplot2 only") +
coord_equal() +
theme_classic() +
theme(panel.border = element_rect(fill = NA)),
ex %>%
group_by(rect) %>%
arrange(x, y) %>%
st_as_sf(coords = c("x", "y")) %>%
summarise() %>%
st_cast(to = "POLYGON") %>%
# st_geometry() %>%
ggplot() +
geom_sf(aes(fill = as.factor(rect)), color = "black", show.legend = FALSE) +
labs(title = "with sf") +
theme_classic() +
theme(panel.border = element_rect(fill = NA)),
align = "h"
)
产生:
和我想要的:
解决方案
这会丢失一些信息,因为我无法弄清楚如何在 dplyr 管道中进行操作。最终的形状在那里,但组Rect
需要相隔超过 1 个单位。“B”组的线段可能会阻止 sf 将其转换为多边形。
一些创造性的加入和分组可能会让你解决上述几个问题。
library(ggplot2)
library(dplyr)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(nngeo)
ex <- data.frame(rect = c(rep("A", 8), rep("B", 6)),
x = c(1.5, 2.5, 1.5, 2.5, 3.5, 1.5, 2.5, 3.5,
5.5, 5.5, 6.5, 6.5, 7.5, 7.5),
y = c(5.5, 5.5, 4.5, 4.5, 4.5, 3.5, 3.5, 3.5,
1.5, 2.5, 2.5, 3.5, 2.5, 3.5))
#Make sf object from dataframe
ex_sf <- st_as_sf(ex, coords = c('x','y'))
#sparse geometry binary predicate list of points within 1 unit of each other,
# needed for st_connect
nn_dist_of_1 <- st_is_within_distance(ex_sf, ex_sf, dist = 1)
connecting_lines <- st_connect(ex_sf, ex_sf, ids = nn_dist_of_1)
#> Calculating nearest IDs
#> Calculating lines
#Plotting
ggplot(ex_sf) + geom_sf(color = 'red', size = 3) +
geom_sf(data = connecting_lines, color = 'black')
#Head of new data objects
nn_dist_of_1
#> Sparse geometry binary predicate list of length 14, where the predicate was `is_within_distance'
#> first 10 elements:
#> 1: 1, 2, 3
#> 2: 1, 2, 4
#> 3: 1, 3, 4, 6
#> 4: 2, 3, 4, 5, 7
#> 5: 4, 5, 8
#> 6: 3, 6, 7
#> 7: 4, 6, 7, 8
#> 8: 5, 7, 8
#> 9: 9, 10
#> 10: 9, 10, 11
head(connecting_lines)
#> Geometry set for 6 features
#> geometry type: LINESTRING
#> dimension: XY
#> bbox: xmin: 1.5 ymin: 4.5 xmax: 2.5 ymax: 5.5
#> CRS: NA
#> First 5 geometries:
#> LINESTRING (1.5 5.5, 1.5 5.5)
#> LINESTRING (1.5 5.5, 2.5 5.5)
#> LINESTRING (1.5 5.5, 1.5 4.5)
#> LINESTRING (2.5 5.5, 1.5 5.5)
#> LINESTRING (2.5 5.5, 2.5 5.5)
由reprex 包于 2021-02-16 创建(v0.3.0)
推荐阅读
- r - 在 R 中重新排列数据框以进行面板分析
- function - javascript对象初始化器的方法定义中'function'关键字的真正作用是什么
- reactjs - 大日历反应我们可以在月份的不同日期有多种颜色吗?
- c# - 我将 c# windows 应用程序与 Access 数据库 (accdb) 连接,但该应用程序在其他计算机上不起作用
- c++ - QString从列表中解析项目
- angular - Angular 6通过服务将组件之间的数据共享到模板组件中
- excel - 保存AS文件,不打开
- php - 路由在 laravel 7 上不起作用,除了默认的一个
- spring - 春季无法提交 LocalTime 列表
- gitlab - 为什么我最近在 Gitlab 中的提交与分支历史不一致?