我正在创建一张包含两层的 map -一张包含生物群落数据的热图(geom_tile),并在顶部分层不同区域的 map .我列出了每个地区的国家列表,我让R来显示一张 map ,其中仅显示不同 colored颜色 的地区的外边界.但是--因为边界是共享的,所以它们只显示一种 colored颜色 ,使得某些区域很难看到.有没有办法让两种 colored颜色 在边界都可见?

注意:我有自己的国家和次地区列表,这里不容易列出,所以我使用的是数据固有的一个

这是 map :

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')
         )

这是我当前的 map -你可以看到红色区域(中非)很难看到,因为旁边是蓝色和紫色.有什么 idea 吗? enter image description here

推荐答案

以下是多种潜在解决方案中的两种,两者都需要一些try 和错误.首先,几点:

  • 在您的repex中,region_RST应该是子区域;
  • 当使用summarise()summarise(geometry = st_union(geometry))时,会生成内部多边形"洞".不确定为什么您的repex没有显示此内容,但这可能是由于包版本不同.无论哪种方式,我都使用了nngeo包函数st_remove_holes()来纠正这一点.

还有其他 Select ,例如使用st_intersection()获取共享边界,然后将其st_cast()传输到LINEWRING,但ne_countries(scale = 50)数据需要大量清理才能获得满意的结果.所以让我们保持简单:

Load packages and generate data:

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") %>%
  group_by(subregion) %>%
  summarise(geometry = st_union(geometry)) %>%
  st_make_valid() %>%
  st_remove_holes() %>%
  ungroup()

Method 1: Dashed lines

  • 优点:保留线宽度,例如linewidth = 1.1
  • 缺点:需要通过改变 colored颜色 和线条进行大量的试错才能获得清晰的结果
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",
                              title.position = "top"))

方法1结果:

method1

@ DelivereGreg建议的Method 2: Negative buffer

  • 优点:边界划分比方法1更清晰
  • 缺点:难以复制线宽度.只有边界会是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",
                              title.position = "top"))

方法2结果:

method2

R相关问答推荐

即使声明引发错误,R函数也会在第二次try 时返回结果

如何在热图中绘制一个图形,但在每个单元格中通过饼形图显示?

从API中抓取R数据SON

在值和NA的行顺序中寻找中断模式

工作流程_set带有Dplyrr风格的 Select 器,用于 Select 结果和预测因子R

判断字符串中数字的连续性

以相同的方式对每个表进行排序

哪一行和行和 Select 特定行,但是考虑到Nas

将标识符赋给事件序列,避免错误观察

扩展R中包含列表的数据框

如何删除R中除数字元素以外的所有元素

如何筛选截止年份之前最后一个测量年度的所有观测值以及截止年份之后所有年份的所有观测值

从线的交点创建面

按组跨多列创建伪变量

构建一个6/49彩票模拟系统

需要一个函数来在第一行创建一个新变量,然后用新变量替换一个不同的变量(对于多行)

按组使用dummy r获取高于标准的行的平均值

R try Catch in the loop-跳过缺少的值并创建一个DF,显示跳过的内容

如何使投篮在R中保持一致

每行不同列上的行求和