How to draw connecting routes on map with R and great circles

This post explains how to draw connection lines between several localizations on a map, using R. The method proposed here relies on the use of the gcIntermediate function from the geosphere package. Instead of making straight lines, it offers to draw the shortest routes, using great circles. A special care is given for situations where cities are very far from each other and where the shortest connection thus passes behind the map.

First we need to load 3 libraries. Maps allows to draw the background map, and geosphere provides the gcintermediate function.


1- Draw an empty map

This is easily done using the ‘maps’ package. You can see plenty of other maps made with R in the map section of the R graph gallery.

2- Add 3 cities

First we create a data frame with the coordinates of Buenos Aires, Melbourne and Paris

Then add it to the map using the ‘points’ function:

4- Show connection between them

Now we can connect cities drawing the shortest route between them. This is done using great circles, what gives a better visualization than using straight lines. This technique has been proposed by Nathan Yau on FlowingData

5 – Correcting gcIntermediate

If we use the same method between Melbourne and Buenos Aires, we get this disappointing result:

What happens is that gcintermediate follows the shortest path, which means it will go East from Australia until the date line, break the line and come back heading East from the pacific to South America. Because we do not want to see the horizontal line, we need to plot this connection in 2 times.

To do so we can use the following function, which breaks the line in 2 sections when the distance between 2 points is longer than 180 degrees.

Let’s try it!

6 – Apply it to several pairs of cities

Let’s consider 8 cities:

We can generate all pairs of coordinates

And plot every connections:

7 – An alternative using the greatCircle function

This is the method proposed by the Simply Statistics Blog to draw a twitter connection map. The idea is to calculate the whole great circle, and keep only the part that stays in front of the map, never going behind it.

Note that the R graph gallery proposes lot of other examples of maps made with R. You can follow the gallery on Twitter or on Facebook to be aware or recent updates.


4 tricks for working with R, Leaflet and Shiny

I recently worked on a dataviz project involving Shiny and the Leaflet library. In this post I give 4 handy tricks we used to improve the app: 1/ how to use leaflet native widgets 2/ how to trigger an action when user clicks on map 3/ how to add a research bar on your map 4/ how to propose a “geolocalize me” button. For each trick, a reproducible code snippet is provided, so you just have to copy and paste it to reproduce the image.

Trick 1: use leaflet native control widget

Continue reading


The Wordcloud2 library

A word cloud (or tag cloud) is a visual representation of text data. Tags are usually single words, and the importance of each tag is shown with font size or color. This mode of representation is useful for quickly perceiving the most prominent terms in a list and determine their relative prominences. In R, two libraries will allow you to create wordclouds: Wordcloud and Wordcloud2. In this post I will introduce the main features of the awesome Wordcloud2 library developed by Chiffon Lang.

Continue reading