This page is dedicated to the creation of background maps with R. Three other sections exist for chloropleth, connection and bubble maps. To make a map, you need the information of the shape of the area that interests you: we call it a spatial object. Several options exist to acquire this info, as presented
in the diagram below. Leaflet and ggmap will provide you a background coming from google. A few R libraries provide the information for common boundaries like world countries. If you are studying a specific area, you will need to find the geospatial information somewhere on the
web, under the Shape file or the GeoJSON format. Once you get this shape information, I advise to use ggplot2 for drawing, or leaflet for interactive representation. Note that dozens of library exist to make map with R, here is just a curated selection of the best performing tools to my opinion.
1.1 – ‘Google background’ + interactive map = the leaflet library
The leaflet library is developed by RStudio. It is probably the easiest way to create a ‘google like’ interactive map in which you can zoom and select any zone. In 3 lines of code. You can also choose your tile in a great number of possibilities. It is especially amazing if you use it with shiny.
#180: 3 lines of code to create an interactive map with R (code)
1.2 – ‘Google background’ + static map = the ggmap library
The ggmap library makes it easy to retrieve raster map tiles from popular online mapping services like Google Maps, OpenStreetMap or Stamen Maps, and plot them using the ggplot2 framework. It produces static maps like these. Click on an image to get the related code snippet.
1.3 – Use data provided in an R library
A few libraries provide the most common spatial objects.. It avoids the struggle to find these information somewhere on the web. The most famous one are: the maps library (Canada, France, Italy, USA and its regions, world cities, NZ…) , the mapdata library (China, Japan, NZ, World in High resolution) and the oz library (Australia).
1.4 – Draw a map from a shape file format
If you are not satisfied with the previous options, you can search the web to find the spatial object you need under the common shape file format. For example, this link provides the shape file of the world countries. The next examples explain how to load this information in R using the rgdal library, and how to represent it using base R or using ggplot2.
1.5 – Draw a map from a GeoJSON format
The geoJSON format is relatively new but is getting more and more popular. Its main advantage is that the geographical information is contained in one unique file. However, its quite large compared to non text format. R allows to read this kind of file using the geojsonio library. Once it is read, you have a spatial polygon data frame object, and you are ready to make your map!
2 – Once you have your geospatial object
Using the examples above, you should now have a geospatial object loaded in R. Whatever it comes from a shape file, a geospatial object or an R library does not matter for the following steps. I advise to plot this object using the ggplot2 library. You can also make an interactive version using the plotly or the leaflet library.