ika*_*sky 21 maps r ggplot2 r-sf
假设我们有两个无缝边界的shapefile.只是,他们没有.有没有办法迫使他们无间隙地相互粘连?
我有两个shapefile:一个用于欧洲地区 - REG另一个用于邻国 - NEI.两个shapefile都是从Eurostat存储库中获取的,应该很好地结合在一起; 但是差距很小.此外,我需要简化多边形,然后差距变得非常显着.
我尝试了几种方法但没有成功.我看到实现所需结果的唯一方法需要执行以下步骤:
NEI;NEI用REGshapefile 剪掉扩展.显然,这是一个相当笨拙的解决方法.
# install dev version of ggplot2
devtools::dev_mode()
devtools::install_github("tidyverse/ggplot2")
library(tidyverse)
library(sf)
library(rmapshaper)
library(ggthemes)
# load data
source(file = url("https://gist.githubusercontent.com/ikashnitsky/4b92f6b9f4bcbd8b2190fb0796fd1ec0/raw/1e281b7bb8ec74c9c9989fe50a87b6021ddbad03/minimal-data.R"))
# test how good they fit together
ggplot() +
geom_sf(data = REG, color = "black", size = .2, fill = NA) +
geom_sf(data = NEI, color = "red", size = .2, fill = NA)+
coord_sf(datum = NA)+
theme_map()
ggsave("test-1.pdf", width = 12, height = 10)
# simplify
REGs <- REG %>% ms_simplify(keep = .5, keep_shapes = TRUE)
NEIs <- NEI %>% ms_simplify(keep = .5, keep_shapes = TRUE)
ggplot() +
geom_sf(data = REGs, color = "black", size = .2, fill = NA) +
geom_sf(data = NEIs, color = "red", size = .2, fill = NA)+
coord_sf(datum = NA)+
theme_map()
ggsave("test-2.pdf", width = 12, height = 10)
Run Code Online (Sandbox Code Playgroud)
ms_simplify似乎适用于您的最小示例,但您需要首先将 2 个“shapefile”分组为一个“shapefile”。如果需要的话,在简化边界后可以很容易地分割它们。
(注意:我的版本在与对象一起使用rmapshaper时会返回错误。这就是为什么我将对象转换为对象的原因)ms_simplifysftmpspas(tmp, "Spatial")
NEI <- st_transform(NEI, st_crs(REG)$epsg)
tmp <- rbind(REG , NEI)
tmp <- ms_simplify(as(tmp, "Spatial"), keep = .1, keep_shapes = T)
ggplot() + geom_sf(data = st_as_sf(tmp)) + theme_bw()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
489 次 |
| 最近记录: |