Geocoding with ggmap and Google API in R

This is a quick R script to visualize spatial information using Google Maps. Before you start, you need to obtain an API key from Google. Go to the registration page (https://developers.google.com/maps/documentation/geocoding/get-api-key) and follow the instructions. I recommend to select all mapping options.

Be aware that the geocoding API is a free service however, you need to associate a credit card with your account.

Step 1 – Load libraries and Google API Key

library(ggmap)
library(ggplot2)
library(ggrepel)

googleAPIkey = scan("/Users/eva/Desktop/googleAPIkey.txt",what="") 
register_google(googleAPIkey)

Step 2 – Load sites with coordinates for mapping

Here I consider 4 sites at Lancaster University but sites and coordinates can be downloaded from a .txt or .csv file.

Name <- c("Sports Centre","Lancaster Environment Centre","Lancaster House","Forrest Hills")
lat <- c(54.012266,54.009148,54.006626,54.007834)
lon <- c( -2.792042,-2.787697,-2.788296,-2.771105)
  
sites <- data.frame(Name,lon,lat) 

4 sites at Lancaster University with associated coordinates:

##                           Name       lon      lat
## 1                Sports Centre -2.792042 54.01227
## 2 Lancaster Environment Centre -2.787697 54.00915
## 3              Lancaster House -2.788296 54.00663
## 4                Forrest Hills -2.771105 54.00783

Step 3 – Create and customise the map

#Create a Google map that covers the area of interest 
myMap <- get_map(location = c(colMeans(sites[,c("lon","lat")])), # centered on the 'center' of all the points
                 source = "google",
                 maptype = "satellite", # can be "roadmap","terrain", "hybrid" check ?get_map 
                 zoom = 14, # map zoom from 3 (continent) to 21 (building), 10-13 for cities.
                 api_key = googleAPIkey) 

#Plot and customise map using ggplot
ggmap(myMap) + 
  #Plot sites
  geom_point(data=sites, aes(x=lon, y=lat), shape = 21, color = "white", fill="red", size=3) + 
  #Name sites
  geom_text_repel(data=sites, aes(label=Name), size=3, color="white", fontface = "bold") 

There is no direct way to make the background transparent but the parameter ‘darken’ can be adjusted:

#Customise darkening
ggmap(myMap,darken=c(0.5,"white")) + 
  #Plot sites
  geom_point(data=sites, aes(x=lon, y=lat), shape = 21, color = "white", fill="red", size=3) + 
  #Name sites
  geom_text_repel(data=sites, aes(label=Name), size=3, color="black", fontface = "bold") 

Step 4 – Exportation

Don’t forget to cite ggmap if you use and publish it! See citation(“ggmap”) for details.

map <- ggmap(myMap,darken=c(0.5,"white")) + 
  #Plot sites
  geom_point(data=sites, aes(x=lon, y=lat), shape = 21, color = "white", fill="red", size=3) + 
  #Name sites
  geom_text_repel(data=sites, aes(label=Name), size=3, color="black", fontface = "bold")   

tiff("myMap_Lancaster_University.tiff", width=15, height=15, units="cm", compression="lzw", res=300)
map
graphics.off()

%d bloggers like this: