Using ggplot to plot pie charts on a geographical map

In this post, we would go through the steps to plot pie charts on a world map, just like the one below.

Plot showing the leading causes of death in the year 2014 for various countries

This image probably scared you as much as it did to me when I realized I need to create something the same as this. “How am I supposed to do this thing when I’m not even good in R programming, in fact, a newbie?” You’re probably asking this to yourself right now, or you already did. I feel you, but I got you. I struggled so much on doing my first map plot, that’s why I’ll be sharing to you the step-by-step ways on how to do it using ggplot, to save you from all the google searches and trial-and-error.

With that, we will be answering a specific question using real-world data as we go along these steps.

“Are there differences in the distribution of causes of death per country?”

I managed to acquire a decent data about death causes in the world per country from kaggle.com which will be used in our illustrations and simulations.

Prepare the Data

The first thing that we need to do is to make sure that our data is complete, that we have what we need prior to plotting. In this case, we need the data for the death causes per country and, the coordinates of each country. Then, we will load it to our work environment as below.

Once we’ve completed all the needed data, we will proceed on deciding what method works for us for the map plot that we will be doing. We have a couple of options to use for ggplot since it is easy to get hold of the map data of the world.

Rdocumentation.org has a very friendly documentation of most of r packages and functions so I would really recommend that you check it out if you’re new to R.

where map is the name of the map provided in the maps package. map_data(“world”) instantly gets the data we need to plot the world map.

where database is the map data you want to plot (check maps::map() for the available maps. borders(“world”) gets the data to create the borders of the world.

Lastly, using a shape (.shp) file. We will need to obtain a shape file of the map that we’re interested at. In this case, I researched of a .shp file for the world and read the data using

where dsn is the shape file we’re interested at. The data obtained from readOGR is not yet ready for plotting. We will use

where “model” is the model or R object that needs to be converted to a data frame. fortify(world) converts the data we got from the .shp file into a useful data frame ready for plotting.

I’ve decided to use all three of them, but you can just choose one on your project, whichever works easiest and best for you.

Basic map plot

The output of these codes looks close to each other.

World map

Adding the pies

In order to add pies to the map plot, we will add a geom_scatterpie function to our original ggplot formula as illustrated below.

World map with the distribution of death causes per country

Adding label, chart title, axis title, etc

To improve the appearance of our visualization, we will add a few more accessories to our chart by adding some new functions to our ggplot formula.

World map with distribution of causes of death per country from various areas

There you go! We have plotted the distribution of death causes per country in the world map. We can see that almost all of them have the same distribution except Belarus and Georgia which is higher in diseases of the circulatory system relative to the rest of the countries.

The method I’ve shown you above is just one way to do it. Other methods for map plot you could try are ggmap, mapplotqtm from tmap package, leaflet, etc. I highly encourage you to also give it a try.

A copy of all the files used on this simulation is available at this repository.

——–

Marriane Makahiya, the author, is a data scientist of SpectData. SpectData is a data analytics company founded to help companies make better use of their data.