Skip to content

Mapmaking for R Programmers

arilamstein edited this page Jun 15, 2014 · 1 revision

Many R programmers working on mapping projects are experts in some statistical domain but lack a background in geography. This is normally fine, but can lead to problems when you have custom needs and do not know how to meet them. During the development of the choroplethr package I was in that situation. The advice that follows is what allowed me to create the custom maps which appear in versions 1.6 and 1.7 of choroplethr. These features took quite a while for me to implement, and my hope is that this document can reduce the time it takes for other people to create similar features.

Learn What a Shapefile is

You can get very technical about it, but at the end of the day a shapefile can be thought of as being the file type for maps. This is analogous to how .html files store webpages and .sql files store SQL queries.

Gain Proficiency with a GIS Program

GIS (Geographic Information System) programs such as QGIS can be thought of as specialized programs for viewing and editing shapefiles (maps). While it is true that R can load shapefiles, there is really no substitue for learning how to use a specialized GIS program.

I recommend downloading QGIS and working thru this video tutorial on YouTube.

Find interesting shapefiles

Now that you have know what a shapefile is and how to use a GIS program, you can search for shapefiles that interest you. choroplethr uses shapefiles which came from these two sources

  1. The US State and Country maps come from the US Census Cartographic Boundary Files webpage.
  2. The world map comes from Natural Earth Data.

This is not meant to be an exaustive list of sites that have high quality, interesting shapefiles. Rather, I am simply pointing you in the direction of two sites which met the needs that I had for choroplethr.

Learn how to import shapefiles into R

I used the above information to get, view and tweak maps for the choroplethr package. But at the end of the day I needed to get these maps into R and render them with ggplot2. Hadley Wickham, the creator of ggplot2 and a ton of other interesting R packages, has a great wiki page on how to do exactly that.

Get a formal education in GIS

The above information helped me to get what I needed done done, and I hope that it does for you as well. But anyone who is self-taught risks making common mistakes and not knowing about better alternatives. For example, choroplethr renders geospatial data as a choropleth map. But what are the inherent limitations of choropleth maps, and what are other alternatives?

The best way I found to get this kind of formal knowledge was Coursera's Maps and the Geospatial Revolution course. The material covered, time commitment and cost were all right, and I recommend the course to other R programmers who are looking to fill in the gaps of their geospatial knowledge.