Mónica Gutierrez
El paquete ggplot2 ha implementado una herramienta muy interesante que permite importar mapas desde Google Maps (la famosa herramienta de Google que muestra múltiples formas de mapas en el mundo). Aquí les mostraré un pequeño abre bocas de lo que hace ésta función.
Primero, hay que instalar la libreria ggmap:
library(knitr)
library(rmarkdown)
library(PKI)
## Loading required package: base64enc
library(packrat)
#install.packages("ggmap")
library(ggmap)
## Loading required package: ggplot2
Encontré dos maneras de buscar el lugar que se quiere mostrar en el mapa. La primera es hacer una busqueda tipo Google del lugar (Forma 1). La segunda (Forma 2), es insertar en un vector, la latitud y longitud del lugar exacto. Para éste ejemplo busqué la “Universidad Santo Tomás” en Bogotá, Colombia.
#Forma 1
localizacion="Universidad santo tomas"
#Forma 2
localizacion=c(lat=4.656748400,lon= -74.056341000)
Una vez que ya está guardado el lugar que deseamos encontrar, es sólo implementar la función.
myMap <- get_map(location=localizacion, zoom= 18, source="google",
maptype="roadmap", crop=TRUE)
## note : locations should be specified in the lon/lat format, not lat/lon.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=4.656748,-74.056341&zoom=18&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
ggmap(myMap)
Lo que necesia la función get_map es la localización básicamente. El zoom lo ajusta automático, pero para que se vea mejor el lugar que se quiere mostrar, es mejor detallarlo. Source y Maptype son dos elementos que van de la mano, ya que Maptype es el tipo de mapa que se elige de Source. Para source encontré dos tipos, “Google” y “Stamen”. Ambas tienen múltiples opciones. Aquí, varios ejemplos de ellos.
myMap <- get_map(location=localizacion, zoom= 18,
source="google", maptype="satellite", crop=TRUE)
## note : locations should be specified in the lon/lat format, not lat/lon.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=4.656748,-74.056341&zoom=18&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
ggmap(myMap)
myMap <- get_map(location=localizacion, zoom= 19,
source="google", maptype="hybrid", crop=TRUE)
## note : locations should be specified in the lon/lat format, not lat/lon.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=4.656748,-74.056341&zoom=19&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
ggmap(myMap)
myMap <- get_map(location=localizacion, zoom= 15,
source="stamen", maptype="watercolor", crop=TRUE)
## note : locations should be specified in the lon/lat format, not lat/lon.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=4.656748,-74.056341&zoom=15&size=640x640&maptype=terrain&sensor=false
## Map from URL : http://tile.stamen.com/watercolor/15/9641/15958.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9642/15958.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9643/15958.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9644/15958.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9641/15959.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9642/15959.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9643/15959.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9644/15959.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9641/15960.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9642/15960.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9643/15960.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/9644/15960.jpg
ggmap(myMap)
myMap <- get_map(location=localizacion, zoom= 15,
source="stamen", maptype="toner", crop=TRUE)
## note : locations should be specified in the lon/lat format, not lat/lon.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=4.656748,-74.056341&zoom=15&size=640x640&maptype=terrain&sensor=false
## Map from URL : http://tile.stamen.com/toner/15/9641/15958.png
## Map from URL : http://tile.stamen.com/toner/15/9642/15958.png
## Map from URL : http://tile.stamen.com/toner/15/9643/15958.png
## Map from URL : http://tile.stamen.com/toner/15/9644/15958.png
## Map from URL : http://tile.stamen.com/toner/15/9641/15959.png
## Map from URL : http://tile.stamen.com/toner/15/9642/15959.png
## Map from URL : http://tile.stamen.com/toner/15/9643/15959.png
## Map from URL : http://tile.stamen.com/toner/15/9644/15959.png
## Map from URL : http://tile.stamen.com/toner/15/9641/15960.png
## Map from URL : http://tile.stamen.com/toner/15/9642/15960.png
## Map from URL : http://tile.stamen.com/toner/15/9643/15960.png
## Map from URL : http://tile.stamen.com/toner/15/9644/15960.png
ggmap(myMap)
Si se realiza una buena búsqueda en internet, encontrará que ésta función tiene muchas opciones. Ésto, es sólo un breve abre bocas.