Mike Bostock is the Pontifex Maximus of data visualization. As a d3.js novice, I spend hours each week poring over his creations over at Observable HQ, a brilliant new medium to share compelling data visualizations and quantitative analysis. And because, as they say, imitation is the highest form of flattery, I’ve decided to reproduce some of his recent work in R (ggplot).
My interest in reproduction, however, goes beyond meer admiration. While I’ve enjoyed getting to know D3, I confess
it’s hard to struggle through the unfamiliar mechanics, knowing I could have produced a ggplot facsimile in a
fraction of the time. I tell myself it builds character. But at a programmatic level, I hope to catch a glimpse of what makes
these visualization paradigms unique and how
easy it is to google their respective errors each tool might be leveraged
in the future. An ambitious goal to be sure.
The visualization I’ve selected for reproduction is U.S. Population by Age and Sex.
What caught my attention was how the interelationship between three variables (Age, Gender, and Population) was visualized so
handsomely within one, two-dimensional space. It’s just a badass chart. All I knew to start was that
I would eventually and somehow use the
fill option in ggplot. The rest was making the way by walking.
Getting the Data
The population data comes from the census API, and whipping up a function and interating through the variables was relatively straightforward.
In brief, I patched together the urls with the formatted variables, iterated through the urls, and pulled out the estimate value. The end goal was always a data frame, but I was still fuzzy on the desired tidyness and shape of the table. I don’t know what “version” this was, but I arrived at the following initial table:
There’s nothing like a little
gathering to make you feel like a magician. Never gets old.
This next code was actually the penultimate block for me. My eventual solution to the estimate labels with different colors was to split the
earlier data into four tables and pass each one into
geom_text. But “chronologically”, I guess, this comes next.
I had also recently watched a presentation on the
zeallot package, and thought I could shoehorn a usage in here:
Confession: I do not believe some of what follows is essential to the final plot, but I lack the patience and courage to go back and change anything now. Here I was pondering how to scale the grey values in the original plot, but the solution jumped out after perusing Bostock’s original code.
Also, as an aside: there was some recent Twitter chatter about the
rowwise() function as an ugly stepchild, and I’ve always wondered why I’m the only one who seems to use it. For me, a mutated value often doesn’t change down a column until I specify the rowwise function.
The end was in sight. I think at this point I’ve already exceeded the number of lines within Bostock’s code, so I’m not winning any points for brevity here. But feast your eyes on this ggplot call!
How’d I do? You decide. A brief explanation:
This chart compares the estimated female and male populations by age in the United States as of 2015. For each age bracket, red represents a larger female population, blue represents a larger male population, and gray represents the smaller of the two. The total estimated population is 316,515,021.
Me and R and ggplot:
Bostock and JS and d3.js
My colors are a little off, I didn’t push the ‘Age’ axis title above the ticks, and nor did I try mirror Bostock’s age axis ticks. But besides those three minor details, I think it’s a faithful imitation!
Now for the reverse: to reproduce a ggplot in D3…