【问题标题】:How can I plot a continents map with R?如何用 R 绘制大陆地图?
【发布时间】:2013-11-22 14:06:38
【问题描述】:

有很多解决方案可以在国家一级绘制地图,但就我而言,我想在大陆一级打印统计数据。

我唯一想到的是使用国家级地图并使用每个大陆的国家列表,但我想知道这种地图是否有任何简单的解决方案。要实现我的想法,应该是这样的:

## produce the world map
map()
## list of countries per continent
SA <- c("argentina", "bolivia", "brazil", "chile", "colombia", "ecuador", "guyana", "paraguay", "peru", "suriname", "uruguay", "venezuela")
map(regions = SA, fill=TRUE, add=TRUE)

【问题讨论】:

  • 寻找大陆形状文件?没有代码,这是对工具/库的离题请求。
  • 我要求 R 代码生成类似图片的内容。
  • @David Ameller 到目前为止最好发布您的代码。
  • 是的,正如@Thomas 所说,第一步是为每个大陆找到一个 shapefile(例如查看 gadm 或 naturalearth)。然后代码很简单:您使用readShapeSpatial 加载shapefile,例如在sp 包中,然后使用非常简单的plot 绘制它们(需要包maptools 来绘制shapefile)。就目前而言,由于您到目前为止还没有展示您的代码,所以这不是一个编程问题。
  • 好的,你可以在这里找到你的大陆 shapefile:baruch.cuny.edu/geoportal/data/esri/world/continent.zip

标签: r plot maps


【解决方案1】:
library(sp) #Load your libraries
library(maptools)
#Download the continents shapefile
download.file("http://baruch.cuny.edu/geoportal/data/esri/world/continent.zip",
              "cont.zip")
#Unzip it
unzip("cont.zip")
#Load it
cont <- readShapeSpatial("continent.shp")
#Plot it
plot(cont,
     col=c("white","black","grey50","red","blue","orange","green","yellow")) 
#Or any other combination of 8 colors

【讨论】:

  • 这个方案不错,但是我发现生成的PDF太重了。此外,我发现必须从外部源下载 shape-file 有点乏味(我也承认它还有其他优点)。
【解决方案2】:

rworldmap 具有将数据绘制或汇总到包括大陆在内的区域级别的功能。

一个简单的开始,应该会产生下面的情节:

library(rworldmap)
#get coarse resolution world from rworldmap
sPDF <- getMap()  
#mapCountries using the 'continent' attribute  
mapCountryData(sPDF, nameColumnToPlot='continent')

或者对于 7 大洲模型:

mapCountryData(sPDF, nameColumnToPlot='REGION')

要将您自己的数据从国家/地区汇总到区域级别,请查看:

?mapByRegion

【讨论】:

  • 您知道您的方法是否有可能获得 7 大洲模型? en.wikipedia.org/wiki/Continent
  • 是的,您可以改用 REGION 属性:mapCountryData(sPDF, nameColumnToPlot='REGION')。我会添加到答案中。这也应该适用于@Josh 出色的国家/地区合并。
  • 我发现以下代码的输出有助于理解各种其他区域指示符,在 ?mapByRegion 中提到但未详细说明:reg&lt;- c("GEO3", "GEO3major", "IMAGE24", "GLOCAF", "Stern", "SRES", "SRESmajor","GBD","AVOIDname"); sapply( reg, function(x) table(sPDF@data[[x]]))
【解决方案3】:

按照@Andy 的回答,您可以像这样合并每个大陆内的国家多边形:

library(rworldmap)
library(rgeos)
library(maptools)
library(cleangeo)  ## For clgeo_Clean()

sPDF <- getMap()
sPDF <- clgeo_Clean(sPDF)  ## Needed to fix up some non-closed polygons 
cont <-
    sapply(levels(sPDF$continent),
           FUN = function(i) {
               ## Merge polygons within a continent
               poly <- gUnionCascaded(subset(sPDF, continent==i))
               ## Give each polygon a unique ID
               poly <- spChFIDs(poly, i)
               ## Make SPDF from SpatialPolygons object
               SpatialPolygonsDataFrame(poly,
                                        data.frame(continent=i, row.names=i))
           },
           USE.NAMES=TRUE)

## Bind the 6 continent-level SPDFs into a single SPDF
cont <- Reduce(spRbind, cont)

## Plot to check that it worked
plot(cont, col=heat.colors(nrow(cont)))

## Check that it worked by looking at the SPDF's data.frame
## (to which you can add attributes you really want to plot on)
data.frame(cont)
#                   continent
# Africa               Africa
# Antarctica       Antarctica
# Australia         Australia
# Eurasia             Eurasia
# North America North America
# South America South America

【讨论】:

  • 感谢 Josh,您的贡献非常有帮助 :)
  • 很高兴为您提供帮助。每个 Polygons 对象应该有一个唯一的 ID 并且 SPDF 的 data.frame 的行名应该与这些 ID 匹配的要求使得这非常棘手,我想我不妨记录它以供将来参考。
  • 嗨 Josh,我可以在 windows 机器上运行你的示例,但不能在 unix 中 R 和 rgeos 的差异版本上运行。请查看此链接并提出建议...提前致谢..stackoverflow.com/questions/41404079/…
  • @Munish -- 看起来 rgeos 自从这个答案最初发布以来,对非封闭多边形变得更加敏感(或者至少这是我最好的猜测)。现在已经编辑了我的答案,包括对cleangeo::clgeo_Clean() 的调用,它解决了这些问题。感谢您指出。
  • 嗨 josh,很抱歉出现错误,但请查看这个问题,有点类似于 R 和/或字段包的版本问题:stackoverflow.com/questions/42149954/…
猜你喜欢
  • 2020-02-09
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
相关资源
最近更新 更多