MazamaSpatialUtils — Ebola Map Example

This entry is part 16 of 21 in the series Using R

The MazamaSpatialUtils package on CRAN has just been updated with additional shape file conversion scripts and location buffering so that points located just outside of polygons (i.e. coastal sites) can still be associated with the nearest neighboring polygon.  This package can now be used as an off-line equivalent for the functionality available at the geonames web service.

In this post I will demonstrate how to use the package to duplicate the kind of high quality maps one sees elsewhere on the internet.


The Ebola Map described in this post was created as a response to a map seen online, created by Robert Johnston. This map seemed interesting to me and I decided to go about recreating it with help from the MazamaSpatialUtils package by Mazama Science. The map details the number of Ebola cases per 1,000 residents in each of the countries of Guinea, Sierra Leone, and Liberia. These cases are comprised of both confirmed and unconfirmed cases, as recorded by the World Health Organization. The data used for the graph was also given on the website. The following shows step-by-step how the MazamaSpatialUtils package can be used as an aid to create informative visual displays from interesting information.

Here is the original map I will be recreating:


Preliminary Mapping

Before beginning, you must of course install and load the MazamaSpatialUtils and sp packages. Utilities within that package will download, convert and load other spatial datasets. The first steps in any project with MazamaSpatialUtils will be:

  1. set the directory to use as the data store
  2. convert any datasets you haven’t already converted
  3. load the desired spatial data into memory as SpatialPolygonsDataFrames

After loading the appropriate data the next step is to identify the border of each country from the TMWorldBorders data. This is done by taking a subset of the TMWorldBorders data that refers to the three countries of interest. The borders between the countries can then be plotted, with thicker lines used to further distinguish the borders.


The next step is to use the convertGADM() function to download and convert the required country data from the GADM database at the correct level. The map uses the provinces of Guinea (level 2), the provinces of Sierra Leone (level 2), and the counties of Liberia (level 1).

Now that each country of interest is divided into provinces/counties, we are ready to proceed to the data acquisition step regarding Ebola cases and their use in our map.

Acquiring Data

Using the rvest package, we can download the necessary table of Ebola counts per province/county into a local dataframe. While the table also includes information for a 2014 population estimate and the total number of Ebola cases, we are solely interested in the column referring to number of cases/1000 pop as this is what we will be graphing.

Using this dataframe, we can sort the cases/100 pop for each province/county into seven levels, which we copy from the original graph in order to achieve as close a match as possible. Note that these levels are not linear. We also used a color picker (e.g. colorzilla) to pull the colors used in the legend of the original graph, again with the goal of providing a match to the original graph. We then sort the data into these seven levels, with corresponding colors. We also add to the end an additional color, the color white, in order to take into account those values from the data which are NA (Values of NA indicate that there were 0 Ebola cases reported, resulting in a NA value for cases/100 pop).

Running this code gives us the error, “NAs introduced by coercion.” We can ignore this error, as we account for the NA values in the next line.

Applying Data to Map

Now that we have sorted our data into categories, we can refer to our preliminary mappings to add our chosen colors to the graph. As we have already labeled the data by level and assigned corresponding colors, we simply plot the GADM data for each country, adding the new plot on top of what we have already created. However, adding the colored polygons for each country has overridden the original borders between the countries, so we must add them back using our original plot containing only the country outlines, not the provinces/counties outlines.

We are almost there! Our graph is looking very similar to the original graph, but it is missing a few crucial pieces. We add a legend defining our use of color, as well as labels for each country, and a title for the graph.

Et voilà!


Variations between the Graphs

Our final product is very similar to the original graph which we were trying to mimic. The main difference lies in the use of color. The original graph appears to use a continuous color scheme, providing a label only for a limited number of the colors used. In contrast, our graph limits itself to six distinct colors, which are used both in the legend and in the graph itself. Aside from this difference, the two plots are quite comparable.

Using the MazamaSpatialUtils package, we were able to choose an existing graph and create our own nearly identical version with just ~35 lines of actual code that did everything from data access and conversion to plotting and annotating.

Have fun creating your own maps of the world!

Series NavigationMazamaSpatialUtils PackageFunction Argument Lists and missing()
This entry was posted in Data Visualization, R and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *