How can I build a map of continents with R?

There are many solutions for building maps at the country level, but in my case I want to print statistics on the continent.

The only thing that comes to my mind is to use country level maps and use the list of countries for each continent, but I was wondering if there was any simple solution for this kind of maps. To implement my idea, this will be as follows:

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

+6
source share
3 answers

rworldmap has functions for building or aggregating data at regional levels, including continents.

A simple start, which should print the chart below:

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

Or for a model of 7 continents:

 mapCountryData(sPDF, nameColumnToPlot='REGION') 

To aggregate your own data from country to regional, see:

 ?mapByRegion 

rworldmap continent map

+12
source

In response to @Andy's answer, you can combine the polygons of the country on each continent as follows:

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

+6
source
 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

+5
source

Source: https://habr.com/ru/post/958643/


All Articles