# 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.

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.

9

Plotly is an R library allowing to make amazing interactive charts in a minute. This blog post shows how to get the best from ggplotly, the function of plotly allowing you to turn any ggplot2 chart interactive. A special care is given on features that are not present in ggplot2 (like hovering), and features that are not well translated by ggplotly (like legend position). Continue reading

2

# 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

2

# The Genetic Map Comparator: a user-friendly application to display and compare genetic maps

The Genetic Map Comparator is an R Shiny application made to compare and characterize genetic maps. You can use it through the online version and read the related publication in Bioinformatics.

The biological perspective

A genetic map provides Continue reading

5

# 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.

19

# Welcome to The R Graph Gallery

Welcome to the R graph gallery! One year ago, I realised that I was tired of learning how to code R charts again and again so I decided to create a reproducible code for each of them. I ultimately designed a new R graph gallery: a website that currently provides more than 200 R graphics and explains how to generate them.

13

# Hello World!

The R graph gallery is almost 1 year old. I am starting a blogpage which will concern dataviz with R of course!

7