I remember someone tweeted recently that a nice way to get into making R packages is to make your first package a customized ggplot theme. Last night I made a first pass at creating a theme based on the NBER’s graphs in its research summaries.
Here’s a typical NBER graph from the May 2017 NBER digest. The rest of this post is basically a reverse engineering exercise in trying to replicate the design of that graph.
First, I wanted to try to understand what even goes into making a theme in the first place. You can do this by typing theme_grey, the default ggplot theme, in the console.
The printout is pretty long, but basically what we see is that theme_grey is a function where you can specify the size and font, but otherwise it comes with a long list of pre-specified settings e.g. for color and spacing. Now, let’s look at theme_bw, ggplot’s black and white theme.
Notice that theme_bw looks a lot like theme_grey but way shorter. Instead of re-specifying all the settings, it just takes theme_grey and overrides some of the original settings with %+replace%. I’m going to use a similar strategy to build up theme_nber.
To get the colors right, I used this website to get the hexadecimal code for the background of the NBER graph (I used #F7F8FC, which I think might be just a touch off). The next code chunk is the theme itself, if you want to play around with it.
Here’s what it looks like compared to theme_grey, and how to use it.
I haven’t replicated everything exactly. Compared to the NBER theme, I think the background is just a touch off. I don’t have an x-axis, and I have a y-axis label. The colors of the actual plots are also different. Instead of blue and red for Afghanistan and Albania, for instance, the NBER color palette would probably use variations of blue. That’s something I might look into in the future, as well as tweaking other defaults, although I feel like using only variations on blue/grey is a little limiting. But otherwise, I think it’s not too shabby for a first pass. Feel free to test it out for your own purposes and let me know if you have suggestions about the coding or design.
Leave a Comment