Maps

The first step is to load enable the libraries to access the Google maps provider. You need to get your own key from your google account.

apiKey <- "Su_clave_de-Google"

Enable libraries and register your key

library(ggplot2)
library(ggmap)
# You should get your own api key from Google. Follow:  https://cloud.google.com/maps-platform/
register_google(apiKey)

Plot quick sample maps using the function qmap. This example uses Google’s gazetteer. You should enable these APIs in Google’s APIs & Services:

  • Geocoding API
  • Maps Embed API
  • Maps JavaScript API
  • Maps Static API
  • Places API
# Quick plot of the City of Cali / Google Maps provider
qmap("Cali", zoom = 10)

# get_googlemap() can access Google Static Maps API and customize the map attributes. See:
# https://developers.google.com/maps/documentation/static-maps/
qmap("Cali", zoom = 10, maptype = "hybrid")

# The same map from a different map provider. See: http://maps.stamen.com/
qmap("Cali", zoom = 10, source = "stamen", maptype = "toner")

Adding data to the map

In this case we are using a dataset from MIO transportation system in the City of Cali.

#Load a map
caliCoordenadas <- c(-76.52788, 3.476013)

city <- get_map(caliCoordenadas, zoom = 12)

# With that data and the long/lat of the center you can get the ggplot2 object used by ggmap to plot the raster image on top.
# str(city)

# Generate the map
caliMap <- ggmap(city)

# Get data from file
MioStops <- read.delim(file="MIO_STOPS.txt")

# Generate the map
caliMap <- ggmap(city)

# Add points to the map
caliMap <- caliMap + geom_point(data = MioStops, aes(x= DECIMALLONGITUDE, y = DECIMALLATITUDE), color = "red", alpha = 0.3)

# Plot the map
caliMap

Zooming

# Cali coords
CaliCoords <- geocode("Chipichape,Cali")

# Retrieve the map fron the provider
cityCenter <- get_map(c(CaliCoords$lon, CaliCoords$lat), source= "stamen", maptype="toner", zoom = 16)

# Generate the map
caliCenter <- ggmap(cityCenter)

# Add points to the map
caliCenter <- caliCenter + geom_point(aes(x= DECIMALLONGITUDE, y = DECIMALLATITUDE),data = MioStops, color = "red")

# Plot the map
caliCenter

Ejercicio

Busque en Google maps la dirección de su casa. Haga un mapa centrado sobre su casa y dibuje los paraderos del Mio mas cercanos. El diámetro de los puntos debe representar el número de pasajeros y el color el tipo de bus.

Publique el resultado en su repositorio