Share the Gallery !Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

 

 

 

 

 

 

This post explains how to make a bubble map with ggplot2. A bubble map is like a bubble chart, but with a map in the background. As input you need a list of GPS coordinates (longitude and latitude of the places you want to represent) + a numerical variable that we will use for the map (and color) of the bubbles. Here I will use the number of inhabitant of the 1000 biggest cities in UK.

 

 

 

 

1 – Load data


As usual, the first step is to get the boundaries of the zone that interests you. Several option are available in R, as extensively described in the background map section of the gallery.

Briefly, you can find this information somewhere under the shapefile format or under the geoJSON format. You can also load google like backgrounds with the ggmap library. Last you can load an R library that provides this kind of information, like the maps package. This is what I do here, and it works for whatever country in the world!

The second step is to load a data frame with the info of the bubble you want to draw. The maps library provides a list of the biggest cities in the world, let’s use it!

 

 

2 – Basic scatterplot map


Now it is quite straightforward to map both information on the same graphic. Use geom_polygon for the shape of UK first, and add your scatterplot on it. On the right figure, note the use of the ggrepel library to avoid overlapping between city names.

 

3 – Use bubbles


Now we want to add another information. The number of inhabitant per city will be mapped to the colour and the size of the bubbles. Note that the order of city matters! It is advised to show the most important information on top (center). This can been done sorting your dataset before making the plot.

 

 

4 – Custom the bubbles


 

 

 

 

 

 

 

We can custom a little bit this figure for a better looking result (first image of this post).

 

Note that here the legend shows both the size, the color and the transparency on the same circles. This is possible only if these 3 informations are redondante, with the same name, transformation and breaks.

 

 

 

 

5 – Interactive version 


 

 

 

 

 

Last but not least, plotly allows to quickly get an interactive version. This is really handy since it allows to zoom on the map and hover a city to know its name and population!



 

Related