Demo R maps

Code for the workshop 'Maps with R' for the R Users Meeting Madrid. Introductory slides.

Before running install the following packages:

install.packages(c("rgeos", "sp", "maptools", "RColorBrewer", "classInt", "rgdal", 
    "ggmap", "googleVis"))

setwd("C:/Users/gonzalo/Downloads/Rmaps")
  1. Read shapefiles into R and plot some meaningful data

Load a Shapefile into R (downladed from madrid.org)

THE PARKS
library(rgeos)
## rgeos version: 0.3-2, (SVN revision 413M)
##  GEOS runtime version: 3.3.8-CAPI-1.7.8 
##  Polygon checking: TRUE
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(sp)

getinfo.shape("Shapefiles/MAD_CM_parques_jardines/parques.shp")
## Shapefile type: Point, (1), # of Shapes: 93

ED50 <- CRS(paste("+proj=utm +zone=30 +ellps=intl +units=m +no_defs"))
# http://cicero.azavea.com/docs/epsg_codes.html equal to 'ED50(ED77) / UTM
# zone 38N' but Marid is in the UTM zone 30

parquesMadrid <- readShapePoints("Shapefiles/MAD_CM_parques_jardines/parques.shp", 
    proj4string = ED50)
str(parquesMadrid, max.level = 2)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  93 obs. of  9 variables:
##   .. ..- attr(*, "data_types")= chr [1:9] "N" "C" "C" "C" ...
##   ..@ coords.nrs : num(0) 
##   ..@ coords     : num [1:93, 1:2] 474496 468752 446390 446099 445527 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   ..@ bbox       : num [1:2, 1:2] 401341 4432124 474496 4502774
##   .. ..- attr(*, "dimnames")=List of 2
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
head(parquesMadrid@data)
##   CODIGO2 CMUN         MUNICIPIO                           ETIQUETA
## 0       1  005 Alcalá de Henares Real Jardín Botánico Juan Carlos I
## 1       2  005 Alcalá de Henares         Parque Municipal O´Donnell
## 2       3  006        Alcobendas                  Jardín de la Vega
## 3       4  006        Alcobendas                 Parque de Cataluña
## 4       5  006        Alcobendas                Parque de Andalucia
## 5       6  007          Alcorcón            Parque Ciudad de Mayarí
##                                                     BUSCA
## 0 Real Jardín Botánico Juan Carlos I (Parques y Jardines)
## 1         Parque Municipal O´Donnell (Parques y Jardines)
## 2                  Jardín de la Vega (Parques y Jardines)
## 3                 Parque de Cataluña (Parques y Jardines)
## 4                Parque de Andalucia (Parques y Jardines)
## 5            Parque Ciudad de Mayarí (Parques y Jardines)
##                                               DIRECCION  UTM_X   UTM_Y
## 0                         Crta. Nacional II, km. 33,800 474496 4486278
## 1                            Calle Vía Complutense, s/n 468752 4481807
## 2 Avda. Olímpica, s/n (N-I, salida 16) c/v Avda. Ermita 446390 4487596
## 3                        Calle Marqués de Valdavia,  23 446099 4488225
## 4          Avda. Valdelaparra, c/v Avda. Pintor Murillo 445527 4488011
## 5                                      Calle Viena, s/n 428843 4467353
##                                                                        INFOR
## 0        Real Jardín Botánico Juan Carlos I<br>Crta. Nacional II, km. 33,800
## 1                   Parque Municipal O´Donnell<br>Calle Vía Complutense, s/n
## 2 Jardín de la Vega<br>Avda. Olímpica, s/n (N-I, salida 16) c/v Avda. Ermita
## 3                       Parque de Cataluña<br>Calle Marqués de Valdavia,  23
## 4        Parque de Andalucia<br>Avda. Valdelaparra, c/v Avda. Pintor Murillo
## 5                                Parque Ciudad de Mayarí<br>Calle Viena, s/n
parquesMadrid@bbox
##               min     max
## coords.x1  401341  474496
## coords.x2 4432124 4502774

parquesMadrid@data <- as.data.frame(apply(parquesMadrid@data, 2, function(x) iconv(x, 
    "latin1", "UTF-8")))
plot(parquesMadrid, col = parquesMadrid$MUNICIPIO)

plot of chunk load_parks


parquesMadrid <- parquesMadrid[parquesMadrid$MUNICIPIO == "Madrid", ]
plot(parquesMadrid, col = parquesMadrid$MUNICIPIO)

plot of chunk load_parks


plot(parquesMadrid, pch = 20, cex = 2, col = "springgreen3")

plot of chunk load_parks

THE NEIGHBORHOODS

getinfo.shape("Shapefiles/MAD_barrios_madrid/barrios_madrid.shp")  # It is a SpatialPolygonsDataFrame
## Shapefile type: Polygon, (5), # of Shapes: 128
barriosMadrid <- readShapePoly("Shapefiles/MAD_barrios_madrid/barrios_madrid.shp", 
    proj4string = ED50)
str(barriosMadrid, max.level = 2)
## Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  128 obs. of  3 variables:
##   .. ..- attr(*, "data_types")= chr [1:3] "N" "C" "C"
##   ..@ polygons   :List of 128
##   .. .. [list output truncated]
##   ..@ plotOrder  : int [1:128] 44 112 114 51 125 52 106 54 127 120 ...
##   ..@ bbox       : num [1:2, 1:2] 424864 4462774 456134 4499579
##   .. ..- attr(*, "dimnames")=List of 2
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

head(coordinates(barriosMadrid))
##     [,1]    [,2]
## 0 439529 4474317
## 1 440497 4473624
## 2 440990 4474246
## 3 441010 4475220
## 4 440129 4475452
## 5 440336 4474520
barriosMadrid@bbox
##       min     max
## x  424864  456134
## y 4462774 4499579
head(barriosMadrid@data)
##   CODBDT GEOCODIGO          DESBDT
## 0 722505    079011     011 Palacio
## 1 722506    079012 012 Embajadores
## 2 722507    079013      013 Cortes
## 3 722508    079014    014 Justicia
## 4 722509    079015 015 Universidad
## 5 722510    079016         016 Sol

plot(barriosMadrid)
plot(parquesMadrid, pch = 20, cex = 2, col = "springgreen3", add = TRUE)

plot of chunk load_barrios

Plot some meaningful data

POPULATION DENSITY

Read the csv file with the Madrid density http://www-2.munimadrid.es/CSE6/control/seleccionDatos?numSerie=14010100010

denMad <- read.csv("Data/denBarriosMad.csv", sep = ";", stringsAsFactors = FALSE)
colnames(denMad) <- c("Barrio", "Densidad")
head(denMad)
##                   Barrio Densidad
## 1 TOTAL CIUDAD DE MADRID       53
## 2             01. CENTRO      265
## 3           01.1 Palacio      155
## 4       01.2 Embajadores      460
## 5            01.3 Cortes      184
## 6          01.4 Justicia      229
# Los barrios tienen una estructura ##.# Barrio (el resto distritos)

denMad <- denMad[which(grepl("[0-9]{2}\\.[0-9]", denMad$Barrio) == TRUE), ]
head(denMad)
##             Barrio Densidad
## 3     01.1 Palacio      155
## 4 01.2 Embajadores      460
## 5      01.3 Cortes      184
## 6    01.4 Justicia      229
## 7 01.5 Universidad      343
## 8         01.6 Sol      176

length(denMad$Barrio)
## [1] 128
length(barriosMadrid$DESBDT)
## [1] 128

Merge the data with the SpatialPolygonDataFrame

head(barriosMadrid@data)
##   CODBDT GEOCODIGO          DESBDT
## 0 722505    079011     011 Palacio
## 1 722506    079012 012 Embajadores
## 2 722507    079013      013 Cortes
## 3 722508    079014    014 Justicia
## 4 722509    079015 015 Universidad
## 5 722510    079016         016 Sol
head(denMad$Barrio)
## [1] "01.1 Palacio"     "01.2 Embajadores" "01.3 Cortes"     
## [4] "01.4 Justicia"    "01.5 Universidad" "01.6 Sol"

# Get ready the Spatial object data to merge
barriosMadrid$DESBDT <- iconv(barriosMadrid$DESBDT, "latin1", "UTF-8")

barriosNamesCodes <- strsplit(sub(" ", "\\.", barriosMadrid$DESBDT), "\\.")
head(barriosNamesCodes)
## [[1]]
## [1] "011"     "Palacio"
## 
## [[2]]
## [1] "012"         "Embajadores"
## 
## [[3]]
## [1] "013"    "Cortes"
## 
## [[4]]
## [1] "014"      "Justicia"
## 
## [[5]]
## [1] "015"         "Universidad"
## 
## [[6]]
## [1] "016" "Sol"

barriosNamesCodes <- do.call(rbind, barriosNamesCodes)
head(barriosNamesCodes)
##      [,1]  [,2]         
## [1,] "011" "Palacio"    
## [2,] "012" "Embajadores"
## [3,] "013" "Cortes"     
## [4,] "014" "Justicia"   
## [5,] "015" "Universidad"
## [6,] "016" "Sol"
barriosMadrid$BarrioCod <- barriosNamesCodes[, 1]
barriosMadrid$Barrio <- barriosNamesCodes[, 2]
head(barriosMadrid@data)
##   CODBDT GEOCODIGO          DESBDT BarrioCod      Barrio
## 0 722505    079011     011 Palacio       011     Palacio
## 1 722506    079012 012 Embajadores       012 Embajadores
## 2 722507    079013      013 Cortes       013      Cortes
## 3 722508    079014    014 Justicia       014    Justicia
## 4 722509    079015 015 Universidad       015 Universidad
## 5 722510    079016         016 Sol       016         Sol


# Get ready the density data.frame to merge
head(denMad)
##             Barrio Densidad
## 3     01.1 Palacio      155
## 4 01.2 Embajadores      460
## 5      01.3 Cortes      184
## 6    01.4 Justicia      229
## 7 01.5 Universidad      343
## 8         01.6 Sol      176
denMad$Barrio <- gsub(".", "", denMad$Barrio, fixed = TRUE)
head(denMad)
##            Barrio Densidad
## 3     011 Palacio      155
## 4 012 Embajadores      460
## 5      013 Cortes      184
## 6    014 Justicia      229
## 7 015 Universidad      343
## 8         016 Sol      176

barriosNamesCodes <- do.call(rbind, strsplit(sub(" ", "\\.", denMad$Barrio), 
    "\\."))
head(barriosNamesCodes)
##      [,1]  [,2]         
## [1,] "011" "Palacio"    
## [2,] "012" "Embajadores"
## [3,] "013" "Cortes"     
## [4,] "014" "Justicia"   
## [5,] "015" "Universidad"
## [6,] "016" "Sol"

denMad$Barrio <- barriosNamesCodes[, 2]
denMad$BarrioCod <- barriosNamesCodes[, 1]
head(denMad)
##        Barrio Densidad BarrioCod
## 3     Palacio      155       011
## 4 Embajadores      460       012
## 5      Cortes      184       013
## 6    Justicia      229       014
## 7 Universidad      343       015
## 8         Sol      176       016
head(barriosMadrid@data)
##   CODBDT GEOCODIGO          DESBDT BarrioCod      Barrio
## 0 722505    079011     011 Palacio       011     Palacio
## 1 722506    079012 012 Embajadores       012 Embajadores
## 2 722507    079013      013 Cortes       013      Cortes
## 3 722508    079014    014 Justicia       014    Justicia
## 4 722509    079015 015 Universidad       015 Universidad
## 5 722510    079016         016 Sol       016         Sol

# Merge the Spatial object data.frame with the density data.frame

identical(sort(barriosMadrid$BarrioCod), sort(denMad$BarrioCod))
## [1] TRUE


barriosMadrid@data <- merge(barriosMadrid@data, denMad[, c(2, 3)])
head(barriosMadrid@data)
##   BarrioCod CODBDT GEOCODIGO          DESBDT      Barrio Densidad
## 1       011 722505    079011     011 Palacio     Palacio      155
## 2       012 722506    079012 012 Embajadores Embajadores      460
## 3       013 722507    079013      013 Cortes      Cortes      184
## 4       014 722508    079014    014 Justicia    Justicia      229
## 5       015 722509    079015 015 Universidad Universidad      343
## 6       016 722510    079016         016 Sol         Sol      176
rm(barriosNamesCodes)

PLOT THE DATA

plot(barriosMadrid, col = barriosMadrid$Densidad)

plot of chunk plot_data



# SP library
library(RColorBrewer)
colors <- colorRampPalette(brewer.pal(9, "YlGnBu"))(16)
spplot(barriosMadrid, "Densidad", col.regions = colors)

plot of chunk plot_data



# CLASS INT library
if (!"classInt" %in% installed.packages()) install.packages("classInt")

library(classInt)

int <- classIntervals(barriosMadrid$Densidad, 10, style = "pretty")
int
## style: pretty
##   one of 5.053e+12 possible partitions of this variable into 10 classes
##    [0,50)  [50,100) [100,150) [150,200) [200,250) [250,300) [300,350) 
##        24        12        13        24        20        14         7 
## [350,400) [400,450) [450,500] 
##         4         7         3
colors <- colorRampPalette(brewer.pal(9, "YlGnBu"))(10)
barriosMadrid$colors <- findColours(int, colors)
head(barriosMadrid@data)
##   BarrioCod CODBDT GEOCODIGO          DESBDT      Barrio Densidad  colors
## 1       011 722505    079011     011 Palacio     Palacio      155 #97D6B8
## 2       012 722506    079012 012 Embajadores Embajadores      460 #081D58
## 3       013 722507    079013      013 Cortes      Cortes      184 #97D6B8
## 4       014 722508    079014    014 Justicia    Justicia      229 #5CC0C0
## 5       015 722509    079015 015 Universidad Universidad      343 #1E80B8
## 6       016 722510    079016         016 Sol         Sol      176 #97D6B8

plot(barriosMadrid, col = barriosMadrid$colors)
legend("bottomleft", cex = 0.7, legend = leglabs(int$brks), fill = colors, title = "Population density Madrid (2013)")

plot of chunk plot_data

PLOT THE NEIGHBORHOOD LABELS

head(coordinates(barriosMadrid))
##     [,1]    [,2]
## 0 439529 4474317
## 1 440497 4473624
## 2 440990 4474246
## 3 441010 4475220
## 4 440129 4475452
## 5 440336 4474520
length(coordinates(barriosMadrid))
## [1] 256

cents <- coordinates(barriosMadrid)

pointLabel(cents[, 1], cents[, 2], labels = barriosMadrid$Barrio)
## Error: plot.new has not been called yet

png("labels.png", width = 2500, height = 2100)
plot(barriosMadrid, col = barriosMadrid$colors)
legend("bottomleft", cex = 0.9, legend = leglabs(int$brks), fill = colors, title = "Population density Madrid (2013)")
plot(parquesMadrid, pch = 20, cex = 4, col = "springgreen3", add = TRUE)
pointLabel(cents[, 1], cents[, 2], labels = barriosMadrid$Barrio)
dev.off()
## pdf 
##   2

PLOT DENSITY AS POINTS

dots <- dotsInPolys(barriosMadrid, barriosMadrid$Densidad)  # Plot as many dots as people living in every polygon
plot(dots, pch = 16, cex = 0.1, col = "violetred3")

plot of chunk den_as_points



plot(barriosMadrid, col = "grey90")
plot(dots, pch = 16, cex = 0.1, col = "violetred3", add = TRUE)
plot(parquesMadrid, pch = 20, cex = 1, col = "springgreen3", add = TRUE)

plot of chunk den_as_points

PLOT THE METRO LINES

metroLines <- readShapeLines("Shapefiles/MAD_lineas_metro/lineas.shp", proj4string = ED50)


plot(barriosMadrid, col = "grey90")
plot(dots, pch = 16, cex = 0.1, col = "violetred3", add = TRUE)
plot(parquesMadrid, pch = 20, cex = 1, col = "springgreen3", add = TRUE)
plot(metroLines, col = "turquoise3", lwd = 2, add = TRUE)

plot of chunk metro

  1. Plot a map on a street map.

GGMAP AND GGPLOT LIBRARIES (also rgdal and rgeos)

1.CHANGE THE COORDINATE SYSTEM, as we have UTM in our objects and ggplot works on long, lat coordinates.

For this purpose, the rgdal library is needed.

library(rgdal)
## rgdal: version: 0.8-11, (SVN revision 479M)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
## Path to GDAL shared files: C:/Program Files/R/R-3.0.2/library/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470]
## Path to PROJ.4 shared files: C:/Program Files/R/R-3.0.2/library/rgdal/proj

str(barriosMadrid, max.level = 2)
## Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  128 obs. of  7 variables:
##   ..@ polygons   :List of 128
##   .. .. [list output truncated]
##   ..@ plotOrder  : int [1:128] 44 112 114 51 125 52 106 54 127 120 ...
##   ..@ bbox       : num [1:2, 1:2] 424864 4462774 456134 4499579
##   .. ..- attr(*, "dimnames")=List of 2
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
barriosMadrid@proj4string
## CRS arguments:
##  +proj=utm +zone=30 +ellps=intl +units=m +no_defs


barriosLongLat <- spTransform(barriosMadrid, CRS("+proj=longlat"))  # Transform the coordinates to lon lat
parquesLongLat <- spTransform(parquesMadrid, CRS("+proj=longlat"))
metroLongLat <- spTransform(metroLines, CRS("+proj=longlat"))  # we need to have told to R already qich were the original projection

2.Create a data.frame from the Spatial**DataFrame data slot, containing the long and lat variables, as ggplot plots data.frames and not Spatial objects

library(ggplot2)

# Points DataFrame to ggplot
head(coordinates(parquesLongLat))
##    coords.x1 coords.x2
## 24    -3.616     40.40
## 25    -3.699     40.48
## 26    -3.710     40.48
## 27    -3.598     40.46
## 28    -3.682     40.39
## 29    -3.655     40.40

long <- coordinates(parquesLongLat)[, 1]
lat <- coordinates(parquesLongLat)[, 2]
dataParques <- cbind(parquesLongLat@data, long, lat)
head(dataParques)
##    CODIGO2 CMUN MUNICIPIO                                     ETIQUETA
## 24      25  079    Madrid Parque Forestal de Valdebernardo (Vicálvaro)
## 25      26  079    Madrid                                 Parque Norte
## 26      27  079    Madrid                         Parque de la Vaguada
## 27      28  079    Madrid        Parque El Capricho (Alameda de Osuna)
## 28      29  079    Madrid        Parque Enrique Tierno Galván (Madrid)
## 29      30  079    Madrid                     Parque Cerro del Tío Pío
##                                                                BUSCA
## 24 Parque Forestal de Valdebernardo (Vicálvaro) (Parques y Jardines)
## 25                                 Parque Norte (Parques y Jardines)
## 26                         Parque de la Vaguada (Parques y Jardines)
## 27        Parque El Capricho (Alameda de Osuna) (Parques y Jardines)
## 28        Parque Enrique Tierno Galván (Madrid) (Parques y Jardines)
## 29                     Parque Cerro del Tío Pío (Parques y Jardines)
##                                              DIRECCION  UTM_X   UTM_Y
## 24                                Avda. Democracia, 29 447759 4472738
## 25                       Avda. Monforte de Lemos , s/n 440788 4481637
## 26                         Avda. Monforte de Lemos, 42 439851 4481396
## 27                               Avda. de Logroño, s/n 449277 4478747
## 28                          Avda. del Planetario , s/n 442119 4471681
## 29 Barrio de Vallecas cerca de la Avda. de la Albufera 444451 4472164
##                                                                              INFOR
## 24            Parque Forestal de Valdebernardo (Vicálvaro)<br>Avda. Democracia, 29
## 25                                   Parque Norte<br>Avda. Monforte de Lemos , s/n
## 26                             Parque de la Vaguada<br>Avda. Monforte de Lemos, 42
## 27                  Parque El Capricho (Alameda de Osuna)<br>Avda. de Logroño, s/n
## 28             Parque Enrique Tierno Galván (Madrid)<br>Avda. del Planetario , s/n
## 29 Parque Cerro del Tío Pío<br>Barrio de Vallecas cerca de la Avda. de la Albufera
##      long   lat
## 24 -3.616 40.40
## 25 -3.699 40.48
## 26 -3.710 40.48
## 27 -3.598 40.46
## 28 -3.682 40.39
## 29 -3.655 40.40

ggplot(dataParques, aes(long, lat)) + geom_point(colour = "springgreen3", size = 4)

plot of chunk ggplot



# Polygons DataFrame to ggplot
head(coordinates(barriosLongLat))
##     [,1]  [,2]
## 0 -3.713 40.42
## 1 -3.701 40.41
## 2 -3.696 40.42
## 3 -3.695 40.42
## 4 -3.706 40.43
## 5 -3.703 40.42
barriosLongLat@data$id <- rownames(barriosLongLat@data)
dataBarrios <- fortify(barriosLongLat, region = "id")  # requires rgeos package. explicitly identifies attribute rows by the .dbf offset.
head(dataBarrios)
##     long   lat order  hole piece group id
## 1 -3.708 40.42     1 FALSE     1   1.1  1
## 2 -3.708 40.42     2 FALSE     1   1.1  1
## 3 -3.708 40.42     3 FALSE     1   1.1  1
## 4 -3.708 40.42     4 FALSE     1   1.1  1
## 5 -3.708 40.42     5 FALSE     1   1.1  1
## 6 -3.708 40.42     6 FALSE     1   1.1  1
class(dataBarrios)
## [1] "data.frame"
dataBarrios <- merge(dataBarrios, barriosLongLat@data)
head(dataBarrios)
##   id   long   lat order  hole piece group BarrioCod CODBDT GEOCODIGO
## 1  1 -3.708 40.42     1 FALSE     1   1.1       011 722505    079011
## 2  1 -3.708 40.42     2 FALSE     1   1.1       011 722505    079011
## 3  1 -3.708 40.42     3 FALSE     1   1.1       011 722505    079011
## 4  1 -3.708 40.42     4 FALSE     1   1.1       011 722505    079011
## 5  1 -3.708 40.42     5 FALSE     1   1.1       011 722505    079011
## 6  1 -3.708 40.42     6 FALSE     1   1.1       011 722505    079011
##        DESBDT  Barrio Densidad  colors
## 1 011 Palacio Palacio      155 #97D6B8
## 2 011 Palacio Palacio      155 #97D6B8
## 3 011 Palacio Palacio      155 #97D6B8
## 4 011 Palacio Palacio      155 #97D6B8
## 5 011 Palacio Palacio      155 #97D6B8
## 6 011 Palacio Palacio      155 #97D6B8


# Lines DataFrame to ggplot
str(coordinates(metroLongLat@lines[[1]]))
## List of 1
##  $ : num [1:168, 1:2] -3.77 -3.77 -3.77 -3.77 -3.77 ...
length(metroLongLat@lines)
## [1] 73
head(coordinates(metroLongLat@lines[[1]]))
## [[1]]
##          [,1]  [,2]
##   [1,] -3.766 40.25
##   [2,] -3.766 40.25
##   [3,] -3.766 40.25
##   [4,] -3.766 40.25
##   [5,] -3.767 40.25
##   [6,] -3.767 40.24
##   [7,] -3.768 40.24
##   [8,] -3.768 40.24
##   [9,] -3.768 40.24
##  [10,] -3.769 40.24
##  [11,] -3.769 40.24
##  [12,] -3.769 40.24
##  [13,] -3.769 40.24
##  [14,] -3.769 40.24
##  [15,] -3.769 40.24
##  [16,] -3.770 40.24
##  [17,] -3.770 40.24
##  [18,] -3.770 40.24
##  [19,] -3.771 40.24
##  [20,] -3.772 40.24
##  [21,] -3.771 40.24
##  [22,] -3.771 40.23
##  [23,] -3.770 40.23
##  [24,] -3.770 40.23
##  [25,] -3.770 40.23
##  [26,] -3.769 40.23
##  [27,] -3.769 40.23
##  [28,] -3.769 40.23
##  [29,] -3.769 40.23
##  [30,] -3.768 40.23
##  [31,] -3.768 40.23
##  [32,] -3.765 40.23
##  [33,] -3.763 40.23
##  [34,] -3.761 40.23
##  [35,] -3.761 40.23
##  [36,] -3.761 40.23
##  [37,] -3.761 40.23
##  [38,] -3.760 40.23
##  [39,] -3.760 40.23
##  [40,] -3.760 40.23
##  [41,] -3.760 40.23
##  [42,] -3.759 40.23
##  [43,] -3.759 40.23
##  [44,] -3.759 40.23
##  [45,] -3.759 40.23
##  [46,] -3.759 40.23
##  [47,] -3.759 40.23
##  [48,] -3.759 40.23
##  [49,] -3.760 40.22
##  [50,] -3.756 40.22
##  [51,] -3.755 40.22
##  [52,] -3.755 40.23
##  [53,] -3.754 40.23
##  [54,] -3.754 40.23
##  [55,] -3.753 40.23
##  [56,] -3.751 40.24
##  [57,] -3.751 40.24
##  [58,] -3.751 40.24
##  [59,] -3.751 40.24
##  [60,] -3.750 40.24
##  [61,] -3.750 40.24
##  [62,] -3.750 40.24
##  [63,] -3.749 40.24
##  [64,] -3.750 40.24
##  [65,] -3.749 40.24
##  [66,] -3.749 40.24
##  [67,] -3.748 40.24
##  [68,] -3.748 40.24
##  [69,] -3.748 40.24
##  [70,] -3.748 40.24
##  [71,] -3.748 40.24
##  [72,] -3.747 40.24
##  [73,] -3.747 40.24
##  [74,] -3.748 40.24
##  [75,] -3.748 40.24
##  [76,] -3.748 40.24
##  [77,] -3.748 40.24
##  [78,] -3.749 40.24
##  [79,] -3.750 40.24
##  [80,] -3.751 40.24
##  [81,] -3.752 40.24
##  [82,] -3.752 40.24
##  [83,] -3.754 40.25
##  [84,] -3.754 40.25
##  [85,] -3.755 40.25
##  [86,] -3.755 40.25
##  [87,] -3.755 40.25
##  [88,] -3.755 40.25
##  [89,] -3.755 40.25
##  [90,] -3.756 40.25
##  [91,] -3.756 40.25
##  [92,] -3.756 40.25
##  [93,] -3.756 40.25
##  [94,] -3.756 40.25
##  [95,] -3.756 40.25
##  [96,] -3.756 40.25
##  [97,] -3.756 40.25
##  [98,] -3.757 40.25
##  [99,] -3.757 40.25
## [100,] -3.757 40.25
## [101,] -3.757 40.25
## [102,] -3.757 40.25
## [103,] -3.757 40.25
## [104,] -3.757 40.25
## [105,] -3.758 40.25
## [106,] -3.760 40.25
## [107,] -3.761 40.25
## [108,] -3.761 40.25
## [109,] -3.761 40.25
## [110,] -3.762 40.25
## [111,] -3.762 40.25
## [112,] -3.762 40.25
## [113,] -3.762 40.25
## [114,] -3.762 40.25
## [115,] -3.763 40.25
## [116,] -3.763 40.25
## [117,] -3.763 40.25
## [118,] -3.763 40.25
## [119,] -3.763 40.25
## [120,] -3.763 40.25
## [121,] -3.763 40.25
## [122,] -3.763 40.25
## [123,] -3.763 40.25
## [124,] -3.763 40.25
## [125,] -3.763 40.25
## [126,] -3.763 40.25
## [127,] -3.763 40.25
## [128,] -3.763 40.25
## [129,] -3.763 40.25
## [130,] -3.764 40.25
## [131,] -3.764 40.25
## [132,] -3.764 40.25
## [133,] -3.764 40.25
## [134,] -3.764 40.25
## [135,] -3.764 40.25
## [136,] -3.764 40.25
## [137,] -3.764 40.25
## [138,] -3.764 40.25
## [139,] -3.764 40.25
## [140,] -3.764 40.25
## [141,] -3.764 40.25
## [142,] -3.764 40.25
## [143,] -3.764 40.25
## [144,] -3.764 40.25
## [145,] -3.764 40.25
## [146,] -3.764 40.25
## [147,] -3.764 40.25
## [148,] -3.764 40.25
## [149,] -3.764 40.25
## [150,] -3.764 40.25
## [151,] -3.764 40.25
## [152,] -3.764 40.25
## [153,] -3.764 40.25
## [154,] -3.765 40.25
## [155,] -3.765 40.25
## [156,] -3.765 40.25
## [157,] -3.765 40.25
## [158,] -3.765 40.25
## [159,] -3.765 40.25
## [160,] -3.765 40.25
## [161,] -3.765 40.25
## [162,] -3.765 40.25
## [163,] -3.765 40.25
## [164,] -3.766 40.25
## [165,] -3.766 40.25
## [166,] -3.766 40.25
## [167,] -3.766 40.25
## [168,] -3.766 40.25


metroLongLat@data$id <- rownames(metroLongLat@data)
dataMetro <- fortify(metroLongLat, region = "id")  # requires rgeos package
head(dataMetro)
##     long   lat order piece group id
## 1 -3.766 40.25     1     1   0.1  0
## 2 -3.766 40.25     2     1   0.1  0
## 3 -3.766 40.25     3     1   0.1  0
## 4 -3.766 40.25     4     1   0.1  0
## 5 -3.767 40.25     5     1   0.1  0
## 6 -3.767 40.24     6     1   0.1  0
class(dataMetro)
## [1] "data.frame"
dataMetro <- merge(dataMetro, metroLongLat@data)
head(dataMetro)
##   id   long   lat order piece group ETIQUETA
## 1  0 -3.766 40.25     1     1   0.1       TP
## 2  0 -3.766 40.25     2     1   0.1       TP
## 3  0 -3.766 40.25     3     1   0.1       TP
## 4  0 -3.766 40.25     4     1   0.1       TP
## 5  0 -3.767 40.25     5     1   0.1       TP
## 6  0 -3.767 40.24     6     1   0.1       TP

3.Once we have data.frames ready plot them on a streetmap

library(ggmap)
MadridMap <- qmap(location = "Museo del Prado", zoom = 15, source = "google")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Museo+del+Prado&zoom=15&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Museo+del+Prado&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
MadridMap

plot of chunk unnamed-chunk-2

MadridMap <- qmap(location = barriosLongLat@bbox, zoom = 11, maptype = "terrain")  # Get the map from Google maps
## converting bounding box to center/zoom specification. (experimental)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=40.478877,-3.702281&zoom=11&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
MadridMap

plot of chunk unnamed-chunk-2



MadridMap + geom_polygon(aes(x = long, y = lat, group = group, fill = Densidad), 
    data = dataBarrios, alpha = 0.5, colour = "white") + scale_fill_gradient(low = "grey90", 
    high = "black", limits = c(0, 500)) + geom_path(aes(x = long, y = lat, group = group), 
    data = dataMetro, colour = "turquoise3") + geom_point(aes(x = long, y = lat), 
    data = dataParques, , size = 5, alpha = 0.8, colour = "springgreen3") + 
    theme(legend.position = "none")

plot of chunk unnamed-chunk-2

  1. Plot a map with ggplot without a background street map.
ggplot() + geom_polygon(aes(x = long, y = lat, group = group, fill = Densidad), 
    data = dataBarrios) + scale_fill_gradient(low = "grey90", high = "black", 
    limits = c(0, 500)) + geom_point(aes(x = long, y = lat, size = 5), data = dataParques, 
    colour = "springgreen3") + geom_path(aes(x = long, y = lat, group = group), 
    data = dataMetro, colour = "turquoise3") + theme(legend.position = "none", 
    panel.background = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank(), 
    text = element_blank())

plot of chunk unnamed-chunk-3

  1. googleVis

More examples at rpubs

library(googleVis)
## 
## Welcome to googleVis version 0.4.5
## 
## Please read the Google API Terms of Use
## before you use the package:
## https://developers.google.com/terms/
## 
## Type ?googleVis to access the overall documentation and
## vignette('googleVis') for the package vignette.
## You can execute a demo of the package via: demo(googleVis)
## 
## More information is available on the googleVis project web-site:
## http://code.google.com/p/google-motion-charts-with-r/
## 
## Contact: <rvisualisation@gmail.com>
## 
## To suppress the this message use:
## suppressPackageStartupMessages(library(googleVis))
emigrantes <- read.csv("Data/emigrantes.csv", sep = ";")
head(emigrantes)
##     X                pais  X2008  X2009  X2010  X2011  X2012
## 1 131    PAIS DESCONOCIDO  52069  94850 134790 138972 146309
## 2 132 BAJAS POR CADUCIDAD 142315 154213 163515 139223 130421
## 3  82             AMERICA  26993  29440  29319  36618  43667
## 4  92             Ecuador   1693   3027   4292   6668  10622
## 5  29         Reino Unido   5186   5339   6249   8237   8055
## 6  17             Francia   4394   4338   5105   6130   6960

## me quedo las que no empiezan por dos mayúsculas
emigrantes <- emigrantes[which(grepl("^[A-Z]{2}", emigrantes$pais) == FALSE), 
    ]
head(emigrantes)
##     X           pais X2008 X2009 X2010 X2011 X2012
## 4  92        Ecuador  1693  3027  4292  6668 10622
## 5  29    Reino Unido  5186  5339  6249  8237  8055
## 6  17        Francia  4394  4338  5105  6130  6960
## 7   8       Alemania  3991  4123  4658  5904  6577
## 10 32       Rumanía  5854  5673  4875  5076  5359
## 11 94 Estados Unidos  4163  3897  3647  4891  4498

emigrantesMap <- gvisGeoChart(emigrantes, locationvar = "pais", colorvar = "X2012", 
    options = list(width = 800, height = 500))
print(emigrantesMap, "chart")

# si queremos exportarlo a un archivo:
cat(emigrantesMap$html$chart, file = "emigrantes2012.html")