R map中的叠加区域边界会生成不需要的行

时间:2017-04-28 16:56:55

标签: r

我已经为尼日利亚各州制作了一张简单的地图,现在我想在我的地图中突出显示尼日利亚地区(尼日利亚各州)的边界。 当我使用geom_polygon为边框添加图层时,它们会显示与区域边框不对应的线条。我在这里发现了类似的问题Map county borders on to a ggmap  但这似乎对我的情况不起作用。

以下是我正在处理的shapefile和数据库: https://www.dropbox.com/sh/cek92s50jixowfx/AABwIVZKvtff8-9slhfCbxEca?dl=0

我正在使用的代码是

#LOAD SHAPEFILES AND DATABASE 
ng_dist <- readShapeSpatial("NGA_adm1.shp")
ng_dist_regions <- readShapeSpatial("NGA_adm_Region.shp")
NG_States <- read.csv("State_color_map.csv", header = TRUE, sep=",")

#VERIFY THE MAPS LOADED PROPERLY
plot(ng_dist)
plot(ng_dist_regions)

# STATE MAP - fortify and match shapefile and database IDs names

intersect(ng_dist$NAME_1, NG_States$STATE)
ng_dist <- fortify(ng_dist, region = "NAME_1")

ng_dist$id[which(ng_dist$id == "Akwa Ibom")] <- "Akwa-ibom"
ng_dist$id[which(ng_dist$id == "Nassarawa")] <- "Nasarawa"
ng_dist$id[which(ng_dist$id == "Cross River")] <- "C/river"
ng_dist$id[which(ng_dist$id == "Federal Capital Territory")] <- "FCT"

intersect(ng_dist$id, NG_States$STATE)


# REGION MAP - fortify
ng_dist_regions <- fortify(ng_dist_regions, region = "Region")


### Convert dummy variable to factor

NG_States$Abia <- as.factor(NG_States$Abia)


#PLOT MAP with coloured Abia State

cols <- c("0" = "#e6e6e6","1" = "#6eab27")

ABIA <- NG_States$Abia

Abia_map <- ggplot(NG_States, aes(fill = ABIA)) + 
  geom_map(data = NG_States, aes(map_id = NG_States$STATE, fill = ABIA), map = ng_dist, color = "black", size = 0.10) + 
  expand_limits(x = ng_dist$long, y = ng_dist$lat) +  
  theme_nothing(legend = FALSE) +
  labs(title="Abia") +
  coord_map() + 
  scale_fill_manual(name="", values=cols, labels=c("","Abia"))

Abia_map 

#Add layer for region borders

d <- Abia_map +   
   geom_polygon(aes(x = ng_dist_regions$long, y = ng_dist_regions$lat, group = ng_dist_regions$id, fill = NA), data = ng_dist_regions, color = "red", size = 0.8) 

d

这是我的结果

Nigerian States and Regions

我尝试添加其他选项,例如coord_fixed()expand_limits(x = ng_dist_regions$long, y = ng_dist_regions$lat),但我是非常基本的R用户,而且我不了解其他解决方案。

1 个答案:

答案 0 :(得分:0)

使用group而不是id作为群组似乎可以解决问题。

d <- Abia_map +   
  geom_path(aes(x = long, y = lat, group = group), data = ng_dist_regions, color = "red", size = 0.8, inherit.aes = FALSE) 
d

enter image description here