R tutorial: plot maps from shapefiles

In my opinion, visualizations are always better than showing data tables. Whenever I work with spatial data, I like to plot the data on a map. There are several ways to visualize shapefile data on a map in R, I will walk you through a couple of them below.

In this tutorial I will use shapefiles to plot spatial data. A shapefile contains all the coordinates and additional data, such as region names. I’ve downloaded a shapefile of Soutch Africa, because I love this country ūüôā You can download the shapefile on¬†http://www.gadm.org/download. This file contains all the coordinates which we are going to plot. In addition, I will also use¬†population data from Wikipedia and coordinates of the five biggest cities of South Africa from Tageo.

 

Load shapefile data and plot map

So let’s start by loading the¬† maptools¬†library and store the location of the .shp file in a new object. Then I load the population data and coordinates of the cities into the right format. Next, I’ll read the shapefile into an object and plot the first map. In order to read and open the shapefile, I’ll use the¬† maptools¬†package.

This simple code already produces the following map:

Plot shapefile in R using maptools package

That wasn’t difficult at all! The shapefile includes all kinds of information. I always start to look what kind of information is included. You can use the¬† @¬†and¬† $¬†signs to extract objects from the data. The following examples show you which information is in the shapefile of South Africa.

 

Color shapefile based on population number

Now we know what kind of information is available and how to plot the first map from the shapefile, it’s time to color the areas. I want to color the provinces according to their population intensity. The file does not include population data, sometimes shapefiles include information like this. But it’s not a problem, because we can add columns to the shapefile.

After adding the population column to the shapefile, I’m going to use the¬† colorRampPalette()¬†function to create HEX color coding in a new column called¬† color¬†. Next, I’ll tell the¬† plot()¬†function to apply this color and to not show the borders.

Plot shapefile in R using maptools package

That looks good but I want to add a legend with the population numbers corresponding to the colors. You can add a legend by using the¬† legend()¬†function. The legend needs to know which colors to plot and the corresponding values, so I’ll create bins of the population data and use the¬† colorRampPalette()¬†¬†again to get the right color codes.

Plot shapefile and legend in R using maptools package

Now we have the colors and legend in place, I want to finish the map with the five biggest cities. We can plot the cities based on their longitude and latitude. We can use the function  points() to add points to the map created with the shapefile. You can change some arguments such as the color and type of point you want to plot.

Plot shapefile, legend and points in R using maptools package

Et voila, there we have our map of South Africa created with a shapefile. With some additional data we added the colors and points on the map.

 

Ggplot2 for shapefiles

But this was just a start, know I will show you how to use¬† ggplot()¬†to create a map with shapefiles. First, we need to activate the library¬† ggplot2¬†to use the function¬† ggplot()¬†.¬† We need to tell the function which shapefile we want to use, but also the longitude and latitude columns, and which column contains the regions. I’ll also add black borders and make sure that the map is plotted using the right scale.

Plot shapefile in R using ggplot2

This looks nice already, but I want to recreate the map with colors based on the population, a legend and points on the map for the five biggest cities.

In order to color the regions, we need to transform the shapefile into a data frame. But I start by reordering the shapefile data, such that the order starts at 1 again. This is something we need to do, because otherwise the coloring will go wrong. As soon as the order of the regions is back to the original order, I’m using the function¬† fortify()¬†to transform the shapefile to a data frame. This creates a data frame with all the coordinates on a new line. Each line/coordinate needs to know the population value, so I merge the data to add a new column containing the population. Then I create an object of the map including the colors for the regions and a legend.

Plot shapefile, colors and legend in R using ggplot2

I want to end by adding some points for the five biggest cities on the map.

Plot shapefile, colors, legend and points in R using ggplot2

So that’s the final map! I’ve shown you two options how you could plot a map from a shapefile. There are more ways to do this, but I think these two are the best to know.

World full of data author

Who I am


Hi! My name is Claudia, a freelance data analyst/scientist. This is my space on the internet where I share knowledge and experience with everyone who wants to become a better analyst. Read more about my work as a freelancer here.

Share this post on

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.