Friday, June 19, 2015

Making Maps with R

  • rworldmap
  • ggmaps

Setup your packages

## If necessary
install.packages(c("rworldmap", "rworldxtra", "RColorBrewer",
    "maptools", "classInt"))
    
## Load packages
library('rworldmap')
library('rworldxtra')
library('RColorBrewer')
library('maptools')
library('classInt')

Load the map data

First we need to load the data. For this we will be using data provided by the package rworldmap for our maps. You can do the same thing with your own map files or using packages that use freely available maps, like Google Maps or OpenStreetMap (https://www.openstreetmap.org).

worldmap <- getMap(resolution = "high")
dim(worldmap)
## [1] 253  51

What's in the data object

names(worldmap)
##  [1] "ne_10m_adm"   "ScaleRank"    "LabelRank"    "FeatureCla"  
##  [5] "OID_"         "SOVEREIGNT"   "SOV_A3"       "ADM0_DIF"    
##  [9] "LEVEL"        "TYPE"         "ADMIN"        "ADM0_A3"     
## [13] "GEOU_DIF"     "GEOUNIT"      "GU_A3"        "SU_DIF"      
## [17] "SUBUNIT"      "SU_A3"        "NAME"         "ABBREV"      
## [21] "POSTAL"       "NAME_FORMA"   "TERR_"        "NAME_SORT"   
## [25] "MAP_COLOR"    "POP_EST"      "GDP_MD_EST"   "FIPS_10_"    
## [29] "ISO_A2"       "ISO_A3"       "ISO_N3"       "ISO3"        
## [33] "LON"          "LAT"          "ISO3.1"       "ADMIN.1"     
## [37] "REGION"       "continent"    "GEO3major"    "GEO3"        
## [41] "IMAGE24"      "GLOCAF"       "Stern"        "SRESmajor"   
## [45] "SRES"         "GBD"          "AVOIDnumeric" "AVOIDname"   
## [49] "LDC"          "SID"          "LLDC"

Plot the world

par(mar=c(0,0,0,0))     # Set 0 margins
plot(worldmap)          # Plot

Plot a smaller area

Setting the xlim and ylim sets our plotted area to a specific limit of lattitude and longitude coordinates.

  • ylim = Lattitude
  • xlim = Longitude

par(mar=c(0,0,0,0))     # Set 0 margins
plot(worldmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1) 

Plot a Region, Country, or other Area

We can also select only certain regions or countries if we want.

t(t(table(worldmap$REGION)))
##                                  
##                                   [,1]
##   Africa                            57
##   Antarctica                         1
##   Asia                              46
##   Australia                         27
##   Europe                            70
##   North America                      5
##   South America and the Caribbean   44

More

table(worldmap$GEO3)
## 
##                 Antarctic         Arabian Peninsula 
##                         1                         6 
## Australia and New Zealand                    Canada 
##                         5                         2 
##                 Caribbean            Central Africa 
##                        23                         9 
##              Central Asia            Central Europe 
##                         5                        21 
##            Eastern Africa            Eastern Europe 
##                         9                         8 
##                   Mashriq              Meso-America 
##                         5                         8 
##              North Africa  NW Pacific and East Asia 
##                         8                         9 
##                     Polar             South America 
##                         1                        13 
##                South Asia             South Pacific 
##                        10                        22 
##            Southeast Asia           Southern Africa 
##                        11                        12 
##                        US            Western Africa 
##                         3                        15 
##            Western Europe      Western Indian Ocean 
##                        40                         4

Plot Europe

par(mar=c(0,0,0,0))     # Set 0 margins
europe <- worldmap[which(worldmap$REGION=="Europe"),]               
plot(europe, col="lightgreen", bg="lightblue")

This looks a little weird because of Russia and the inclusion of the island territories.

Europe excluding Russia and Territories

par(mar=c(0,0,0,0))  
europe <- worldmap[which(grepl("Europe", worldmap$GEO3) & 
                             as.character(worldmap$NAME) != "Russia"),]               
plot(europe, col="lightgreen", bg="lightblue")