首页 > 解决方案 > 使用 dplyr 查找多边形的中心

问题描述

我正在制作一张连接美国密苏里州县之间弧线的地图。我通过取每个多边形的经度/纬度的平均值来计算每个县的“足够好”的中心。这适用于或多或少呈方形的县,但对于形状更复杂的县则不太适用。我认为这一定很常见,但我无法在线或使用我创建的任何功能找到答案。我想使用一个 tidyverse 工作流程(即,如果我能提供帮助,不要转换为空间对象)。手头的问题是否有任何 tidyverse 解决方案。

您可以在下面的示例中看到问题。

library(tidyverse)

# import all state/county fortified
all_states <- as_tibble(map_data('county'))

# filter for missouri
mo_fortify <- all_states %>%
  filter(region == 'missouri')

## Pull Iron county, which is relatively oddly shaped
mo_iron_fortify <- mo_fortify %>% 
 group_by(subregion) %>% 
  mutate(long_c =  mean(long),
         lat_c = mean(lat),
         iron = ifelse(subregion == 'iron','Iron','Others')) %>% 
  ungroup()  

# map a ggplot2 map
mo_iron_fortify %>% 
  ggplot(aes(long, lat, group = group))+
  geom_polygon(aes(fill = iron), 
               color = 'white')+
  geom_point(aes(long_c, lat_c))+
  scale_fill_grey('Iron county is\na good example')+
  coord_map()+
  theme_bw()

上面代码中的示例

标签: rdplyrspatial

解决方案


推荐阅读