2013-11-22 45 views
6

在国家层面绘制地图有许多解决方案,但在我的情况下,我想在大陆级别打印统计数据。我怎样才能用R来绘制大陆地图?

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

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

worldmap

+0

查找各大洲形状文件?没有代码,这是针对工具/库的脱离主题的请求。 – Thomas

+0

我要求R代码生成类似图片的东西。 –

+1

@David Ameller到目前为止,发布代码会更好。 –

回答

12

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 

rworldmap continent map

+0

你知道是否可以用你的方法获得7大洲模型? http://en.wikipedia.org/wiki/Continent –

+0

是的,你可以使用REGION属性来代替:'mapCountryData(sPDF,nameColumnToPlot ='REGION')'。我会加入答案。这也应该与@乔希卓越的国家合并。 – Andy

+0

我发现以下代码的输出有助于理解各种其他区域指示符,但不详细说明在?mapByRegion中:'reg < - c(“GEO3”,“GEO3major”,“IMAGE24”,“GLOCAF”,“Stern “,”SRES“,”SRESmajor“,”GBD“,”AVOIDname“); sapply(reg,function(x)table(sPDF @ data [[x]]))' –

5
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 

enter image description here

+0

这个解决方案很好,但是我发现生成的PDF太重了。此外,我发现有一点乏味,不得不从外部源下载形状文件(我也承认它具有其他优点)。 –

6

跟进@安迪的回答,您可以在每个大陆内部合并国多边形像这样:

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 

enter image description here

+0

感谢Josh,您的贡献非常有帮助:) –

+0

很高兴为您提供帮助。每个“Polygons”对象都应具有唯一ID和“SPDF”数据的要求。框架的rownames应该匹配那些ID使这足够棘手,我想我不妨将它记录下来供将来参考。 –

+0

嗨乔希,我可以在Windows机器上运行你的例子,但不能在R和RIX的diff版本中运行。请参阅此链接并建议...在此先感谢..http://stackoverflow.com/questions/41404079/using-rgeos-library-to-merge-country-polygons – Munish