A smooth intro to map visualization in R

Marco Michelangeli
Aug 23, 2017 · 4 min read
Stamen watercolor map: Stamen.com

Lately I found myself working a bit with geo data, learning many things of a niche, but very interesting field! After all this work therefore I have decided to share what I have learned by writing a small series of posts (I hope to have time for it!) aimed to introduce newby to this field of work. I presuppose only that you know a bit of R and its main packages.

But lets get started! In this first post I would like to smoothly introduce the R packages that are mostly used with geodata and show how easy is to plot points on map!

We will use the following packages:

library(dplyr)
library(ggmap)
library(jsonlite)

ggmap() and qmap()

First package to introduce is ggmap. It is simply a wrapper for mainly the google map API (it also support Stamen and OpenStreet among others).The main functions of ggmap are get_map() and ggmap(). Give them a location coordinates and the map is yours!

# First get Milan coordinates
Milan<-c(lat=45.464211, lon=9.191383)
#Extract the map from Google map at a specific zoom (this is up to you!)
mapMilan <- get_map(location=Milan, zoom=10)
#Visualize the map
ggmap(mapMilan)
Map of Milan using ggmap()

This is awesome and super easy! And it will get easier: ggmap has another function for doing the same operation in less coding, it is called qmap() and works as follows:

mapMilan<-qmap("Milan", zoom = 10)
mapMilan
Map of Milan using qmap()

No need for coordinates and we get a one step process (get map + visualization). As we do with ggplot() and plot() we need to decide if we want a more complete function (ggmap()), with more freedom for visualizing multiple layers of data or if we want a quick and fast function that just spits out a map (qmap()).

ggmap customization

The customization of ggmap is almost endless. Here I will point you out just that we can have different looking maps using different sources or different google map types. As an example let us look at the same map above using a satellite map type

#Get the map
mapMilanSat <- get_map(location=Milan, zoom=10, maptype = "satellite")
#Visualize the map
ggmap(mapMilanSat)
Map of Milan using ggmap(): satellite

Or a map from Stamen instead of Google:

#Get the map
mapMilanStamen <- get_map(location=Milan, zoom=10, source= "stamen", maptype = "toner-2011")
#Visualize the map
ggmap(mapMilanStamen)
Map of Milan using ggmap(): stamen Toner

Nice right? If you want to know more about possible sources and map types or other ggmap customization just type ?ggmap on your console!

data layer

But wait…what is the usefulness of a map without data on it? So yes, we need to do a step forward and add some point positions to our map layer.

First let’s generate some data. We will get the coordinates of 20 groceries in Milan from google places API and add them to a data frame. If you do not want to bother using this data, just grab a dataframe with coordinates position and skip ahead!

#We will use jsonlite library to connect to Google API
library(jsonlite)

#Store the Google Place API (you need to sign up on their website)
key<-"Place here your key"

#Compose the first part of the API call
plcurl<-"https://maps.googleapis.com/maps/api/place/textsearch/json?location="

#Store the coordinates around which Google will look for groceries
location.lat <-"45.464211"
location.lng <-"9.191383"
location<-paste(location.lat,",",location.lng)

#Tell Goggle what is the radius (Km) within which it has to look
radius<-"50000"

#Tell Google what to look for
query<-"groceries"

#Structure the request and send it!
strurl<-as.character(paste(plcurl,location, "&query=", query,"&radius=",radius,"&key=",key, sep=""))

rd<-fromJSON(URLencode(strurl))
addresses<-rd$results$formatted_address

#Google return a JSOn so we need to put it into a dataframe
coords <- data.frame(Longitude = as.numeric(as.character(rd$results$geometry$location$lng)),Latitude = as.numeric(as.character(rd$results$geometry$location$lat)),Address=as.character(rd$results$formatted_address), stringsAsFactors = FALSE)

Our dataframe looks like this:

head(coords,3)

This coordinates can be easily superimposed to our map layer using (as in ggplot()) geom_point():

mapMilan +
geom_point(data=coords, aes(x=Longitude,y=Latitude), color="red", size=3,alpha=0.5)
Map of Milan using ggmap() with a data layer on it

How easy was that?? And this is just the beginning, ggmap() opens the door to many possibilities for using geo data in R. In the next posts I will be writing more about what geo data are and how to handle and visualize them in a more elegant and beautiful way.

If you want to have a look at the code posted, have look at https://github.com/mmichela/geoviz

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade