Simple use of the
Last year I blogged about making a choropleth map of the Pacific, dealing with a few technical details like getting the map centred on the Pacific, using exclusive economic zones (EEZs) to colour things in for good visibility, adding the international dateline, etc.
Soon after that blog post I abstracted that work and added it to the
frs R package, my miscellaneous collection of bits and pieces associated with this Free Range Statistics blog. I reasoned that as I will be in the Pacific for at least a few years I am likely to want to keep drawing these maps, and they might be useful at work too. You can see:
- the code for the function itself; and
- preparation of the various data objects called by the function.
It’s easy to install with
Here’s the minimal demo of the function. By default it will draw all the countries’ EEZs grey:
Post continues after R code
For a more realistic use case, here’s a map showing the Human Development Index or HDI for 2021, for all Pacific countries that have one:
You can see that the HDI, which is produced by the UNDP, only is calculated for countries that are full UN members; so members of the Realm of New Zealand (Niue, Tokelau and Cook Islands) or French overseas territories (New Caledonia, French Polynesia and Wallis and Futuna) for example aren’t included.
On the other hand, Nauru is a full member of the UN, but it is one of four such countries that lack an HDI score because it is missing data on one of the components - in Nauru’s case, mean years of education. The other countries missing an HDI score for similar reasons are North Korea, Monaco and Somalia.
Here’s the code that downloads that HDI data and draws the map:
Post continues after R code
You can see that it’s non trivial to get a good combination of colours that works complete with the country labels. There are options for most of the things you’d want to change in the initial call to
draw_pac_map itself; and because it outputs a
ggplot2 object you can modify it by adding scales, labels, themes etc the usual way (as I do in the above by adding
scale_fill_viridis_c() to set the scale of colours to use for the fill aesthetic).
Mental note to self - it would be entirely possible to use the data on SDGs 3, 4.3, 4.4 and 8.5 we have in the Pacific Data Hub to create estimates of HDI for at least some of the territories that the UNDP doesn’t calculate it for. But that’s enough of a job to leave for another time.
Combining with Pacific Data Hub data for an animation of SDGs
OK, here’s a more complex use case. I wanted to show off the Pacific Data Hub at a presentation last year to attendees of the Pacific Community’s Committee of Representatives of Governments and Administrations - effectively the board of the Pacific Community or SPC, where I work. I decided it would be nice to do this while also showing that there is a lot of work to be done to improve official statistics production in the region. So I opted to use a sequence of maps, pulling data from the Pacific Data Hub for all the Sustainable Development Goal (SDG) indicators that the Pacific had agreed were priorities to measure. I limited it to those that made sense to represent on a one-dimensional choropleth map (e.g. where a single number for each country is a meaningful thing to compare).
So the result was this GIF, which basically becomes eye candy in the background while I talked about the issues:
I’m going to present all the code that does this animation in one chunk rather than try to explain it line by line. Hopefully it makes sense as-is. The key, like any animation, is to save one frame at a time; then I use ImageMagick to knit them all up into an actual animation. You’ll see I opted to not clutter up this particular set of maps with country labels.
That’s it! See you next time.