MAPS WITH R

The maps library

In order to plot maps in R, we need to install the 'maps' library.
Take a look to ?maps to review all the arguments.
The documentation for the maps package is here

if (!"maps" %in% installed.packages()) install.packages("maps")
library(maps)

1. Ploting a map

We can make a simple world map with just one command:

map()

plot of chunk unnamed-chunk-1

Plots the 'world' map as it is the default value for the 'database' argument

2. Changing the appearance

> Show only some of the lines

map("world", interior = FALSE)  # Without the interior divisions

plot of chunk unnamed-chunk-2

map("world", boundary = FALSE)  # Without the boundary outline (just plot the interior divisions)

plot of chunk unnamed-chunk-2

> Change the lines' colour

map("world", fill = FALSE, col = "#B4045F")

plot of chunk unnamed-chunk-3

As 'fill' is equal to 'FALSE' (the default value), the 'col' argument applies to the lines

> Plot the lines in different colours

map("world", interior = FALSE)  # Plot a map without the interior lines
map("world", boundary = FALSE, interior = TRUE, col = "#B4045F", add = TRUE)  # Add the interior in the desired colour. As the 'add' argument is TRUE add this command to the current plot. If FALSE a new plot is begun.

plot of chunk unnamed-chunk-4

> The fill colour

map("world", interior = FALSE, fill = TRUE, col = "#B4045F")  # As 'fill' is equal to 'TRUE', the 'col' argument applies to the country fill.

plot of chunk unnamed-chunk-5

Although the 'ìnterior' argument is set to FALSE, black outlines are plotted by defoutl when fill is equal to TRUE and a value is given to the 'col' argument.

> Managing the fill and the lines' colour

map('world', interior = FALSE, fill = TRUE, col = '#B4045F')
map('world', interior = TRUE, fill= FALSE, col = 'white', add = TRUE)  # Changing a filled map's line's colour, overplots the black outline. 

plot of chunk unnamed-chunk-6

# In order to avoid the overplotting, you can set the 'lty' argument to 0 in the first call to fill the countries without drawing the black boundaries.
map('world', interior = FALSE, fill = TRUE, col = '#B4045F', lty = 0)
map('world', interior = TRUE, fill= FALSE, col = 'white', add = TRUE)

plot of chunk unnamed-chunk-7

It admits all 'lty' and 'lwd' values from 'par'. See ?par | ??lty | ??lwd

3. Getting the information inside the map object

> If you do not plot the map, you get the latitude and longitude parameters (x/y) for drawing the map (object class = map)

map("state", "tennessee", fill = TRUE, col = "white", plot = FALSE)
## $x
##   [1] -89.72 -89.71 -89.69 -89.65 -89.63 -89.60 -89.60 -89.61 -89.67 -89.69
##  [11] -89.66 -89.61 -89.57 -89.55 -89.55 -89.61 -89.60 -89.59 -89.56 -89.54
##  [21] -89.52 -89.51 -89.55 -89.54 -89.51 -89.51 -89.54 -89.56 -89.55 -89.52
##  [31] -89.48 -89.47 -89.48 -89.45 -89.43 -89.40 -89.40 -89.40 -89.34 -88.85
##  [41] -88.81 -88.52 -88.48 -88.06 -88.05 -88.04 -88.04 -88.05 -88.06 -87.94
##  [51] -87.85 -87.83 -87.83 -87.68 -87.65 -87.32 -87.12 -87.07 -86.76 -86.67
##  [61] -86.60 -86.56 -86.53 -86.50 -86.45 -86.38 -86.19 -85.97 -85.75 -85.45
##  [71] -85.30 -85.28 -84.96 -84.79 -84.72 -84.21 -84.02 -83.75 -83.68 -83.66
##  [81] -83.65 -83.63 -83.47 -82.99 -82.84 -82.60 -82.29 -81.93 -81.90 -81.82
##  [91] -81.65 -81.65 -81.68 -81.70 -81.71 -81.70 -81.70 -81.72 -81.74 -81.72
## [101] -81.72 -81.72 -81.76 -81.80 -81.83 -81.87 -81.93 -81.93 -81.98 -82.01
## [111] -82.05 -82.08 -82.12 -82.13 -82.15 -82.17 -82.22 -82.25 -82.28 -82.32
## [121] -82.35 -82.39 -82.40 -82.41 -82.44 -82.48 -82.51 -82.55 -82.58 -82.61
## [131] -82.62 -82.62 -82.61 -82.64 -82.66 -82.75 -82.79 -82.79 -82.80 -82.83
## [141] -82.87 -82.89 -82.92 -82.91 -82.90 -82.91 -82.94 -82.96 -83.00 -83.02
## [151] -83.06 -83.11 -83.16 -83.22 -83.26 -83.28 -83.30 -83.33 -83.35 -83.37
## [161] -83.40 -83.43 -83.45 -83.49 -83.51 -83.59 -83.66 -83.68 -83.74 -83.79
## [171] -83.84 -83.90 -83.95 -83.98 -84.01 -84.01 -84.01 -84.02 -84.02 -84.02
## [181] -84.05 -84.09 -84.13 -84.17 -84.21 -84.22 -84.24 -84.29 -84.29 -84.32
## [191] -84.33 -84.63 -84.77 -84.83 -84.99 -85.27 -85.36 -85.46 -85.61 -85.85
## [201] -86.31 -86.78 -86.83 -87.20 -87.23 -87.61 -87.99 -88.18 -88.19 -88.37
## [211] -88.40 -88.79 -88.83 -89.02 -89.20 -89.35 -89.64 -89.73 -90.28 -90.29
## [221] -90.27 -90.26 -90.22 -90.21 -90.20 -90.18 -90.15 -90.12 -90.10 -90.09
## [231] -90.06 -90.07 -90.07 -90.06 -90.07 -90.09 -90.10 -90.10 -90.09 -90.10
## [241] -90.11 -90.15 -90.17 -90.19 -90.19 -90.18 -90.16 -90.14 -90.13 -90.12
## [251] -90.10 -90.09 -90.08 -90.07 -90.05 -90.03 -90.01 -89.99 -89.98 -89.96
## [261] -89.91 -89.91 -89.91 -89.90 -89.90 -89.87 -89.86 -89.87 -89.91 -89.94
## [271] -89.94 -89.94 -89.91 -89.83 -89.80 -89.80 -89.78 -89.74 -89.73 -89.73
## [281] -89.74 -89.74 -89.71 -89.66 -89.65 -89.66 -89.69 -89.71 -89.72
## 
## $y
##   [1] 36.00 36.03 36.06 36.06 36.10 36.14 36.17 36.19 36.22 36.23 36.26
##  [12] 36.26 36.25 36.26 36.28 36.33 36.34 36.35 36.37 36.36 36.37 36.39
##  [23] 36.44 36.49 36.50 36.52 36.54 36.55 36.58 36.58 36.57 36.54 36.47
##  [34] 36.46 36.47 36.50 36.50 36.50 36.50 36.50 36.51 36.50 36.51 36.50
##  [45] 36.51 36.56 36.59 36.63 36.68 36.67 36.66 36.66 36.64 36.64 36.63
##  [56] 36.64 36.64 36.64 36.64 36.65 36.65 36.63 36.63 36.65 36.66 36.65
##  [67] 36.64 36.63 36.62 36.62 36.63 36.63 36.62 36.61 36.61 36.58 36.58
##  [78] 36.58 36.58 36.59 36.60 36.60 36.59 36.59 36.58 36.59 36.59 36.60
##  [89] 36.62 36.62 36.62 36.59 36.58 36.55 36.53 36.50 36.47 36.44 36.41
## [100] 36.38 36.37 36.34 36.34 36.35 36.35 36.33 36.26 36.26 36.21 36.14
## [111] 36.12 36.10 36.11 36.11 36.13 36.14 36.14 36.12 36.12 36.12 36.11
## [122] 36.08 36.06 36.04 36.02 35.99 35.98 35.96 35.96 35.97 35.98 36.00
## [133] 36.03 36.06 36.06 36.00 35.98 35.96 35.94 35.93 35.95 35.94 35.92
## [144] 35.89 35.87 35.86 35.82 35.80 35.79 35.79 35.79 35.77 35.75 35.72
## [155] 35.70 35.68 35.67 35.67 35.66 35.64 35.63 35.62 35.60 35.57 35.56
## [166] 35.57 35.57 35.57 35.56 35.54 35.53 35.49 35.46 35.43 35.41 35.37
## [177] 35.35 35.33 35.31 35.29 35.28 35.25 35.24 35.24 35.24 35.25 35.27
## [188] 35.24 35.22 35.00 34.98 34.98 34.98 34.98 34.98 34.98 34.98 34.98
## [199] 34.98 34.98 34.99 34.99 35.00 35.00 35.00 35.01 35.01 35.01 35.00
## [210] 35.00 35.00 35.00 35.00 34.99 35.00 34.99 35.00 34.99 35.00 35.02
## [221] 35.04 35.04 35.04 35.06 35.08 35.09 35.08 35.08 35.09 35.11 35.14
## [232] 35.16 35.18 35.20 35.22 35.25 35.31 35.35 35.36 35.37 35.40 35.37
## [243] 35.37 35.39 35.40 35.41 35.43 35.44 35.45 35.47 35.48 35.47 35.43
## [254] 35.41 35.41 35.42 35.44 35.49 35.52 35.52 35.52 35.53 35.55 35.57
## [265] 35.60 35.64 35.66 35.67 35.67 35.69 35.71 35.74 35.75 35.76 35.78
## [276] 35.79 35.80 35.82 35.83 35.86 35.88 35.90 35.91 35.91 35.92 35.95
## [287] 35.97 35.99 36.00
## 
## $range
## [1] -90.29 -81.65  34.98  36.68
## 
## $names
## [1] "tennessee"
## 
## attr(,"class")
## [1] "map"

> Setting the argument 'namesonly' to TRUE, you get only the region's names that form the called region.

head(map("world", fill = TRUE, col = "white", namesonly = TRUE, plot = FALSE), 
    30)
##  [1] "Canada"                               
##  [2] "South Africa"                         
##  [3] "Denmark"                              
##  [4] "Great Lakes:Superior, Huron, Michigan"
##  [5] "USSR"                                 
##  [6] "Pakistan"                             
##  [7] "Aral Sea"                             
##  [8] "Italy"                                
##  [9] "Nicaragua"                            
## [10] "Netherlands"                          
## [11] "Caspian Sea"                          
## [12] "Mexico"                               
## [13] "Iraq:Hawr al Hammar"                  
## [14] "Iraq"                                 
## [15] "Nicaragua:Lago de Nicaragua"          
## [16] "Lake Malawi"                          
## [17] "Ethiopia"                             
## [18] "Netherlands:IJsselmeer"               
## [19] "USA"                                  
## [20] "Mongolia"                             
## [21] "Switzerland"                          
## [22] "Cambodia"                             
## [23] "France"                               
## [24] "Papua New Guinea:Wuvulu"              
## [25] "USA:Alaska"                           
## [26] "Guatemala"                            
## [27] "Belize"                               
## [28] "Honduras"                             
## [29] "El Salvador"                          
## [30] "Costa Rica"
# USA states
map("state", fill = TRUE, col = "white", namesonly = TRUE, plot = FALSE)
##  [1] "alabama"                         "arizona"                        
##  [3] "arkansas"                        "california"                     
##  [5] "colorado"                        "connecticut"                    
##  [7] "delaware"                        "district of columbia"           
##  [9] "florida"                         "georgia"                        
## [11] "idaho"                           "illinois"                       
## [13] "indiana"                         "iowa"                           
## [15] "kansas"                          "kentucky"                       
## [17] "louisiana"                       "maine"                          
## [19] "maryland"                        "massachusetts:martha's vineyard"
## [21] "massachusetts:main"              "massachusetts:nantucket"        
## [23] "michigan:north"                  "michigan:south"                 
## [25] "minnesota"                       "mississippi"                    
## [27] "missouri"                        "montana"                        
## [29] "nebraska"                        "nevada"                         
## [31] "new hampshire"                   "new jersey"                     
## [33] "new mexico"                      "new york:manhattan"             
## [35] "new york:main"                   "new york:staten island"         
## [37] "new york:long island"            "north carolina:knotts"          
## [39] "north carolina:main"             "north carolina:spit"            
## [41] "north dakota"                    "ohio"                           
## [43] "oklahoma"                        "oregon"                         
## [45] "pennsylvania"                    "rhode island"                   
## [47] "south carolina"                  "south dakota"                   
## [49] "tennessee"                       "texas"                          
## [51] "utah"                            "vermont"                        
## [53] "virginia:chesapeake"             "virginia:chincoteague"          
## [55] "virginia:main"                   "washington:san juan island"     
## [57] "washington:lopez island"         "washington:orcas island"        
## [59] "washington:whidbey island"       "washington:main"                
## [61] "west virginia"                   "wisconsin"                      
## [63] "wyoming"
# The 'usa' value for database argument plots only the external boundary,
# so its regions are not the States
map("usa", fill = TRUE, col = "white", namesonly = TRUE)

plot of chunk unnamed-chunk-9

map("usa", fill = TRUE, col = "white", namesonly = TRUE, plot = FALSE)  # The resuts are not the States
##  [1] "main"              "martha's vineyard" "nantucket island" 
##  [4] "manhattan"         "staten island"     "long island"      
##  [7] "san juan island"   "lopez island"      "orcas island"     
## [10] "whidbey island"
# If only one region is called, prints the name of the region.
map("state", "tennessee", fill = TRUE, col = "white", namesonly = TRUE, plot = FALSE)
## [1] "tennessee"

4. Plotting selected countries/areas

world <- map("world", plot = FALSE, namesonly = T)  # Assign the world's regions' names to a new object we called 'world'
canaryIslands <- world[grep("[Cc]anary.*", world)]  # Search for all the regions containing the 'canary' word
canaryIslands
## [1] "Canary Islands:Gomera"        "Canary Islands:Fuerteventura"
## [3] "Canary Islands:Gran Canaria"  "Canary Islands:Hierro"       
## [5] "Canary Islands:Tenerife"      "Canary Islands:Lanzarote"    
## [7] "Canary Islands:La Palma"
map("world", regions = c(canaryIslands, "Spain"), fill = T, col = "white", lty = 0, 
    bg = "lightblue")

plot of chunk unnamed-chunk-10

> Another way is by selecting a frame giving the longitude (xlim) and latitude(ylim) parameters

map("world", fill = TRUE, col = "white", lty = 0, bg = "lightblue", xlim = c(-15, 
    8), ylim = c(35, 45))
map("world", fill = FALSE, interior = TRUE, col = "grey", add = TRUE)

plot of chunk unnamed-chunk-11

# Search for the rigth longitude and latitude ranges on Google