【问题标题】:Polygons out of centroids in R with sf带有sf的R中质心外的多边形
【发布时间】:2021-02-16 12:26:46
【问题描述】:

我有一组质心,其 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"
)

产生:

以及我想要的:

【问题讨论】:

    标签: r sf


    【解决方案1】:

    这会丢失一些信息,因为我无法弄清楚如何在 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 package (v0.3.0) 于 2021-02-16 创建

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多