r - 使用 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()
解决方案
推荐阅读
- gradle - 如何删除分配的测试类
- python - 在树视图中隐藏行 Odoo
- python-3.x - sphinx 生成的文档:也可以在源代码中进行搜索
- javascript - 我无法更改我的谷歌图表的注释颜色
- bash - 如何在 bash 脚本菜单中使用 q 作为退出?
- java - 我还能在 XML 中使用 smart_banner 吗?
- r - 带有外部和嵌入式 yaml 的 RMarkdown
- swift - 在 swift 中使用 @propertyWrapper 替代隐式解包可选
- frameworks - 所有后端框架都支持微服务吗?
- php - 使用 2 个参数 codeigniter 更新数据