【问题标题】:Remove countries' political borders from ggplot2 map从 ggplot2 地图中删除国家的政治边界
【发布时间】:2020-05-28 07:50:06
【问题描述】:

我需要从以下ggplot2 地图中删除国家/地区的政治边界:

library(ggplot2)

world = map_data('world')

plot=ggplot() +
geom_polygon(data=world, aes(x=long, y=lat, group=group), fill='NA', color='black', size=0.2)

print(plot)

关于我如何做到这一点的任何建议? 谢谢

【问题讨论】:

  • 什么是政治边界?你的意思是你只想显示大陆?
  • 是的,我只想展示大陆。没有例如美国-加拿大、巴西-秘鲁、中国-俄罗斯等之间的边界。
  • 必须是ggplot2吗?在这种情况下,您可以使填充颜色和边框颜色都相同吗?
  • 你可以试试ggplot(world, aes(x=long, y=lat, group = group)) + geom_polygon(col=NA, lwd=3, fill = "white")
  • 或者,找到一个只显示大陆的 shapefile,比如这里 baruch.cuny.edu/confluence/display/geoportal

标签: ggplot2 country borderless world-map


【解决方案1】:

您的问题有两种解决方法:

第一个解决方法:使用地图而不是 ggplot2

library(maps)    
world <- maps::map("world", fill=FALSE, plot=TRUE, interior = FALSE)

结果:

第二种解决方法:使用地图和 ggplot2

library(maps)
library(magrittr)
library(maptools)
library(raster)
library(ggplot2)

#Defining a general CRS
mycrs <- "+proj=longlat +datum=WGS84 +no_defs"

#Using the original maps package, then converting map into SpatialPolygons object
world <- maps::map("world", fill=TRUE) %$% 
  maptools::map2SpatialPolygons(., IDs=names,proj4string=CRS(mycrs))

#The resulting map has self intersection problems so any further operation reports errors; using buffers of width 0 is a fast fix
while(rgeos::gIsValid(world)==FALSE){
  world <- rgeos::gBuffer(world, byid = TRUE, width = 0, quadsegs = 5, capStyle = "ROUND")
}

#Dissolving polygon's limits
world <- raster::aggregate(world)

#Plotting. I add theme_void to your code to erase any axis, etc
ggplot() +
  geom_polygon(data = world, aes(x=long, y=lat, group=group), fill='NA', color='black', size=0.2)+
  theme_void()

结果:

希望对你有帮助

【讨论】:

    【解决方案2】:

    rnaturalearth 包现在提供了一种在 ggplot2 中制作世界地图的更简单方法。

    library(ggplot2)
    library(rnaturalearth)
    
    #Mapping for coastlines
    coast <- ne_coastline(scale = "small", returnclass = "sf")
    
    ggplot(data = coast) + geom_sf() + theme_classic()
    
    #Optional - apply a fill to the continents
    world <- ne_countries(scale = "small", returnclass = "sf")
    
    ggplot(data = world) +
      geom_sf(color = "#E5E5E5", fill = "#E5E5E5") +
      geom_sf(data = coast) + theme_classic()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多