How to create Hans Rosling???s famous animated bubble chart

In my earlier days of learning to code, I decided that I’d like to try to create Hans Rosling’s Gapminder bubble chart — made famous by his hugely entertaining lectures and TED talks — from scratch in R.

I had a few criteria in mind as I proceeded:

  1. Can I do it using economic data straight from the World Bank source, without needing to use local data files or package data which may not be up to date?
  2. Can I make it look reasonably close to how Rosling’s chart looked?
  3. Is it possible do to it in a single piped command in R?

I was able to fulfil all my criteria, and having done so I realized that this was a great little learning exercise for those who wish to work more in animated graphics, and also made me aware of the wbstats package in R which I had never known about before. So I’m sharing it here.

If you can’t be bothered reading this, you can go straight to the code on Github.

As a precautionary note, this isn’t the shortest way of doing it. You can do it in fewer characters, but this will mean not namespacing functions (which I always try to do for the benefit of code readers), or pulling data from intermediate sources. But I think this is a nice, independent and future-proof approach which is clean and efficient.

Setting up and grabbing data

You’ll need the following R packages for this: dplyrggplot2gganimateviridis to help with color, and wbstats to get the data from the World Bank. If you want it to look exactly like mine, you may also need to install the Oswald font family from Google fonts, but this is not critical.

http://Read More