The US Census makes a number of its databases available to developers via the Census API. One of those databases is the International Data Base, in which the Census department provides historic demographic breakdowns (population by age and sex) for many countries, along with projections through 2050. Kyle Walker created the R package idbr (currently only available on GitHub) to make it easy to download these datasets using R, and used it to create the animation below showing Japan's demographic change since 1990. As you can see, the Census Bureau predicts that this ageing of the population will only intensify over time. Note the ever-declining population of babies being added at the bottom of the chart. In fact, Japan's population declined for the first time just this year.

Kyle used ggplot's geom_bar plot to visulize the population pyramid, and rendered it using the Economist theme from the ggthemes package. To run the code, you'll need to request a Census API key (you just need to provide your company and email address, and you'll have one in minutes), and install ImageMagick to support the animation package. You can see Kyle's original code below (I adapted it slightly to show the historical demographics since 1990 and to resize the animation for this blog post.)

For more on Kyle's idbr package, check out the GitHub repository linked below.

Github (walkerke): idbr, An R interface to the US Census Bureau International Data Base API (via the author)