如何用R绘制大陆地图?

时间:2013-11-22 14:06:39

标签: r plot maps

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

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

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

3 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:6)

跟随@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

enter image description here

答案 2 :(得分: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