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.

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

  1. Can we do the same with vehicles routes (defined by source/destination cities or a series of GPS points) ?


  2. What is lon in the plot_my_connection function?

    lines(subset(inter, lon>=0), …)
    lines(subset(inter, lon<0), …)

    I am running into an error as lon is not defined anywhere. Kindly guide please.

  3. Thank you for the informative tutorial. Is there any way to show some values upon the arrows on hovering??Like if some goods are being exported from China to Australia then hovering over the arrow will give some value like total export amount

  4. The sharing is very useful! Thanks a lot! But I still have one question. How to add arrows to these lines? From the origin to destination?

      1. tinter <- tail(inter,2)
        arrows(tinter[1,1], tinter[1,2], tinter[2,1], tinter[2,2], length=0.1, col = "slateblue" )

        this works for me

  5. This R tutorial is very helpful for beginners. I like that you took use through all the steps of creating a map to connecting the routes. I am trying to use a similar program for connecting locations of leads on our prospecting tools. Also, thanks for the tip on the R Graph Gallery. I will follow it too.

  6. This is so clear and informative tutorial!.

    I was wondering if there is any way to assign thickness/thiness to the lines ?

    Say there are 10 flights from Paris to Buenos Aires , I would like to plot ten lines originating from Paris into Buenos Aires with one color. Etc.
    Say there are 8 flights from bangkok to singapore, I would like to plot 8 lines originating from bangkok into Singapore with another color.

    Can we do this in R? if so how? Thank you in advance


    1. Hi Vivek.

      Thanks for your comment, I’m glad this little tutorial was useful to you.

      You can definitely achieve what you need with R. For example, you could play with the lwd argument of the line function to control the thickness of each line. If you have hips of lines, I suggest to plot thin lines first, thick ones last.

      Actually I am preparing a tutorial for that that I plan to publish in the next few weeks, so keep in touch!


Leave a Reply

Your email address will not be published.