A Quick Start to Maps in R

If you’re new to R, and are eager to quickly start mapping away your geo data, without getting into the intricacies of spatial polygons and such, this post may be a good starting point. There are various packages in R for mapping, but here I introduce you to two of my favorite libraries that I find super-easy to use. One for static maps, that you can use with ggplot, and another for interactive maps.

Static Maps with get_map and ggmap

To start with, you need a basemap. It’s easy to fetch one from OpenStreetMap, Google Maps, Stamen, or Naver with the ggmap function get_map, which acts as a wrapper for querying their servers. The first argument of the function should specify one of the following location details: Name of the region, or coordinates of the map’s center-point, or the boundary coordinates of the extent of the map. Additionally, you need to supply maptype, source and zoom. ggmap can then be used to display the basemap and layered with ggplot to plot your data over the map as shown in the examples below.

install.packages("ggmap")

Example 1: Fetch Map with Location Name

library(ggmap)

m <- get_map("Jakarta",zoom=12,maptype="terrain",source="google")
ggmap(m)

Different sources, maptypes and additionally arguments for get_map are described here.

Example 2: Fetch Boundary Specified Map

Instead of the location name, you may specify the coordinates of the extent of a region with make_bbox, and supply it as the primary argument to get_map.

lat <- c(12.9,13.05)
long <- c(77.52,77.7)
bbox <- make_bbox(long,lat,f=0.05)
b <- get_map(bbox,maptype="toner-lite",source="stamen")

ggmap(b)

Example 3: ggmap + ggplot

The thing I love about ggmap is that it works great with ggplot. The data used in this example can be downloaded from here, or better still, use the code below to directly read into R with read_csv from Hadley’s readr library . Note that the url is the raw csv file.

library(readr)

df <- read_csv("https://raw.githubusercontent.com/fastah/sample-data/master/FastahDatasetMapsTutorial.csv")
head(df)
# A tibble: 6 x 5
X1 lat lon Operator Class
<int> <dbl> <dbl> <chr> <chr>
1 1 12.96031 77.64483 Airtel good
2 2 12.96853 77.68258 Airtel good
3 3 12.91277 77.55891 Airtel good
4 4 12.98880 77.51994 Airtel good
5 5 12.90642 77.58555 Airtel good
6 6 12.97968 77.71443 Airtel good

The sample is a tiny subset of the data I use at work, and has details of pings from smartphones that measure the network quality of an operator in a region. As you can see, the csv file contains location coordinates (lat,lon) of pings received, Operator and Class (of network quality). The pings in this sample are from Bangalore, India. Now we can plot the ping latitude-longitudes over the basemap we obtained from Stamen in example 2. For this, we use ggplot, denoting different operators with different colors.

library(ggplot2)

ggmap(b) + geom_point(data = df,
aes(lon,lat,color=Operator),size=2,alpha=0.7) +
labs(x = "Longitude", y = "Latitude",
title="Ping Locations", color = "Operator")

Interactive Maps with mapview

The best way to get started with beautiful, interactive maps in R is with mapview.

install.packages("mapview")

Example 4

Let’s plot the same dataset as before using mapview.

df <- read_csv("https://raw.githubusercontent.com/fastah/sample-data/master/FastahDatasetMapsTutorial.csv")

library(mapview)

coordinates(df) <- ~ lon + lat
proj4string(df) <- "+init=epsg:4326"

mapview(df)

The functions coordinatesand projection from R’s sp library converts the dataset into spatial objects that mapview supports. In particular, coordinates specifies the latitude and longitude of the data, and proj4string creates the projection layer, i.e. the coordinate system. EPSG: 4326 means that the coordinates are latitude-longitude pairs on a reference ellipsoid given by WGS84, projected with Mercator Projection.

You can pan, zoom, change layers, background map styles and hover over points to see details.

The extent of the map is derived by mapview from the input latitude longitude data.

Example 5

To color the points according to one of the columns, say by Operator in the sample dataset, use zcol and burst as follows. zcol specifies attribute name(s) or column number(s), and burst determines whether to show all layers (TRUE) or just one (FALSE).

mapview(df, zcol = "Operator", burst = TRUE)

You can add color palettes, images, popup stuff, gifs, and other fun things to your map. You can also make 3D maps with the cubeView function. One of the drawbacks of mapview is its difficulty in handling large datasets, but there are ways to get around it. For instance, by using maxpoints to set the maximum number of point features displayed . Toy around with mapview’s features here, and post your findings and questions in the comments.

Happy mapping! :)

References:

  1. Overview of Coordinate Reference Systems in R
  2. https://cran.r-project.org/web/packages/mapview/mapview.pdf
  3. https://environmentalinformatics-marburg.github.io/mapview/popups/html/popups.html