MAPS WITH R

The sp library

First, the sp library should be downloaded and loaded.

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

This time, we will use the spatial data provided by GADM, a free spatial database of the location of the world's administrative areas (or administrative boundaries). The site provides map information as native R objects that can be plotted directly with the use of the sp library. Browse the data by country here and select the R(SpatialPolygonsDataFrame) format.

load(url("http://gadm.org/data/rda/ESP_adm1.RData"))  # Creates and stores in memory an object called ´gadm´ which includes  the boundary data of spanish administrative regions.

adm0 means the country w/o interior boundaries
adm1 means 'Comunidades Autónomas'
adm2 means 'Provincias'
adm3 means 'Comarcas'
adm4 means 'Municipios'

esp1 <- gadm  # we can swap the `gadm` name with the more descriptive `esp1`.

# I don't know why, but it is not possible to do this directly
esp1 <- load(url("http://gadm.org/data/rda/ESP_adm1.RData"))
str(esp1)
##  chr "gadm"
# While
class(gadm)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"

Anyway, plot the map

require(RColorBrewer)
spplot(esp1, "Shape_Area")  # Shape_Area is one of the `esp1` object variables, so it fills the map basing on this variable.

plot of chunk unnamed-chunk-5

We could use another variable. E.g. to plot the map based on the NAME_1 variable (the administrative regions' names):

esp1$NAME_1 <- as.factor(iconv(as.character(esp1$NAME_1), "latin1", "UTF-8"))  # Convert the encoding to UTF-8 in order to avoid the problems with 'tildes' and 'eñes'.

spplot(esp1, "NAME_1", col.regions = colorRampPalette(brewer.pal(12, "Set3"))(18), 
    col = "white")  # Plot the 'NAME_1' form the 'esp1' object.

plot of chunk unnamed-chunk-6

# Set the 'col.regions' argument to change the regions colors.  And
# the'col' argument to choose the lines' colors.

It is also possible to attach a new variable in order to plot it.
E.g. Spanish population density by region.

Download the file densityCA.csv that is already cleaned and transformed to match with the esp1 format

fileURL <- "https://dl.dropboxusercontent.com/s/bc5w00p9iw4na4i/densityCA.csv?token_hash=AAGYE7pXXUWHDNjcJ70imi764P8SYfYNEtLDgsJHU7-fYg&dl=1"
download.file(fileURL, destfile = "./densityCA.csv", method = "curl")
densityCA <- read.csv("densityCA.csv", header = TRUE)
densityCA
##                        Region Density
## 1                   andalucía   96.46
## 2                      aragón   28.28
## 3                   cantabria  111.49
## 4             castilla y león   27.02
## 5          castilla-la mancha   26.70
## 6                    cataluña  235.92
## 7             ceuta y melilla 5168.86
## 8         comunidad de madrid  809.52
## 9  comunidad foral de navarra   62.04
## 10       comunidad valenciana  220.57
## 11                extremadura   26.62
## 12                    galicia   94.05
## 13             islas baleares  224.26
## 14             islas canarias  284.46
## 15                   la rioja   64.14
## 16                 país vasco  303.32
## 17     principado de asturias  101.61
## 18           región de murcia  130.32

Attach the Density variable to esp1

esp1$Region <- tolower(esp1$NAME_1)  # Create a temporary variable with the 'NAME_1' variable in lower letters in order to merge both data frames.

esp1@data <- merge(esp1@data, densityCA)

esp1$Region <- NULL  # Remove the temporary variable

Plot the map 'esp1' based on the 'Density' variable

spplot(esp1, "Density", col.regions = colorRampPalette(brewer.pal(9, "YlGnBu"))(18), 
    col = "#081D58", main = "Spanish population density by CC.AA. (2012)")

plot of chunk unnamed-chunk-9

As the 'Ceuta y Melila' density is very high, it´s hard to see the diferences between regions.
We can set the density for this regions to 0 in order to better appreciate the diferences.

esp1$Density[which(esp1$NAME_1 == "Ceuta y Melilla")] <- 0
spplot(esp1, "Density", col.regions = colorRampPalette(brewer.pal(9, "YlGnBu"))(18), 
    col = "#081D58", main = "Spanish population density by CC.AA. (2012)")

plot of chunk unnamed-chunk-10