How can I plot a continents map with R?

前端 未结 3 1133
傲寒
傲寒 2021-01-04 14:34

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

The only thing that comes into my mind is to us

3条回答
  •  醉酒成梦
    2021-01-04 15:04

    Following up on @Andy's answer, you could merge country polygons within each continent like so:

    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

提交回复
热议问题