我正在创建一个具有两层的地图 - 带有生物群落数据的热图(geom_tile),并在顶部分层不同区域的地图。我有每个地区的国家/地区列表,并且我让 R 显示一张地图,其中仅以不同颜色显示各地区的外边界。但是-因为边界是共享的,所以它们只显示一种颜色,使得某些区域很难看到。有没有办法让两种颜色在边框处都可见?
注意:我有自己的国家和次区域列表,这里不容易列出,所以我使用的是数据固有的一个列表
这是地图:
library(rnaturalearthdata)
library(dplyr)
library(ggplot2)
library(sf)
data1<-ne_countries(scale=50)
crs_target<-st_crs ("+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84")
data2<-data1%>%
sf::st_transform(crs=crs_target)
africa<-subset(data2,continent=='Africa')
westA<-africa%>%
group_by(region_wb)%>%
summarise()
ggplot()+
#geom_tile(data=test_cou,aes(x=x,y=y,fill=Biome))+ #My biome data- not relavant
geom_sf(data=westA,fill=NA,aes(colour=region_wb),linewidth=1.1)+
scale_color_manual(values=c('chocolate3','red3','purple3','maroon3','blue3'))+
#scale_fill_manual(values=viridis(12))+
guides(color=guide_legend(position='bottom',direction='horizontal')
)
以下是许多潜在解决方案中的两个,两者都需要一些尝试和错误。首先,几点:
summarise()
或 summarise(geometry = st_union(geometry))
时,会生成内部多边形“孔”。不确定为什么你的 repex 没有显示这一点,但这可能是由于软件包版本不同所致。不管怎样,我已经使用了 nngeo
包函数 st_remove_holes()
来纠正这个问题。还有其他选项,例如使用
st_intersection()
获取共享边框,然后 st_cast()
将它们转为 LINESTRING,但是 ne_countries(scale = 50)
数据需要大量清理才能获得满意的结果。所以让我们保持简单:
加载包并生成数据:
library(dplyr)
library(nngeo) # Remove internal 'holes', also loads the sf package
library(rnaturalearth)
library(ggplot2)
westA <- ne_countries(scale = 50) %>%
st_transform("+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84") %>%
filter(continent == "Africa") %>%
select(sovereignt, subregion) %>%
group_by(subregion) %>%
summarise(geometry = st_union(geometry)) %>%
st_make_valid() %>%
st_remove_holes() %>%
ungroup()
方法一:虚线
linewidth = 1.1
ggplot() +
geom_sf(data = westA,
aes(colour = subregion),
fill = NA,
linewidth = 1.1) +
geom_sf(data = westA,
aes(colour = subregion),
fill = NA,
linewidth = 1.1,
linetype = "dashed") +
scale_color_manual(name = "Subregions",
values=c("chocolate3", "red3", "purple3", "maroon3", "blue3")) +
guides(color = guide_legend(position = "bottom",
direction = "horizontal",
label.position = "bottom",
title.position = "top"))
方法一结果:
方法2:负缓冲,如@VinceGreg建议
linewidth = 1.1
,外部边界显得更细。大量的尝试和错误才找到一个好的缓冲距离westA1 <- st_buffer(westA, -16000)
ggplot() +
geom_sf(data = westA,
aes(colour = subregion),
fill = NA,
linewidth = 0.65) +
geom_sf(data = westA1,
aes(colour = subregion),
fill = NA,
linewidth = .65) +
scale_color_manual(name = "Subregions",
values=c("chocolate3", "red3", "purple3", "maroon3", "blue3")) +
guides(color = guide_legend(position = "bottom",
direction = "horizontal",
label.position = "bottom",
title.position = "top"))
方法2结果: