Working with shapefiles, projections and world maps in ggplot

May 23, 2013

In this post I will show some different examples of how to work with map projections and how to plot the maps using ggplot. Many maps that are shown using their default projection are in the longlat-format, which is far from optimal. For plotting world maps I prefer to use either Robinson or Winkel Tripel projection—but many more are available—and I will show how to use both these projections.

Before we get started you need to download a couple of shapefiles that we will use. You can find them here:

Put them directly inside your working directory. We will use functions from the rgdal-package to read the shapefiles into R, so if you do not have it, you need to install it before you continue.

This will create a longlat-projected world map. World map in ggplot

Here the world map is shown using the Robinson projection. World map in ggplot with robinson projection

However, the Caspian sea is missing. This is because of how ggplot handles polygon holes. Ggplot will plot polygon holes as a separate polygon, thus we need to make it pseudo-transparent by changing its fill color.

![World map in ggplot2 polygon hole example][./img/map3.png]

Now the Caspian sea is visible. World map in ggplot polygon hole fix

If we want we can also add a graticule and a bounding box. The bounding box is useful if we want to make the sea blue—especially when using some form of curved projection. Here I have added a graticule and bounding box to the longlat-map.

World map in ggplot plus graticule and bounding box

Robinson projection with added graticule and bounding box. World map in ggplot using robinson projection with graticule and bounding box

Here I have added country borders to the previous map plot. World map in ggplot in robinson projection with country borders

Bubble plots are a popular way of displaying information on maps. Here I used project() to reproject the bubbles’ coordinates into the Robinson projection. World map in ggplot using robinson projection plus bubble plot

Lastly, here is an example of the Winkel tripel projection. This projection became popular after 1998 when the National Geographic Society choose to use it for their world maps—using it to replace the Robinson projection, which they previously used.
World map in ggplot using winkel tripel projection