R – rud.is, and kindly contributed to Want to share your content on R-bloggers? [This article was first published on, and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is a follow up to a twitter-gist post & to the annotation party we’re having this week

I had not intended this to be “Annotation Week” but there was a large, positive response to my annotation “hack” post. This reaction surprised me, then someone pointed me to this link and also noted that if having to do subtitles via hacks or Illustrator annoyed me, imagine the reaction to people who actually do real work. That led me to pull up my ggplot2 fork (what, you don’t keep a fork of ggplot2 handy, too?) and work out how to augment ggplot2 -proper with the functionality. It’s yet-another nod to Hadley as he designed the package so well that slipping in annotations to the label, theme & plot-building code was an actual magical experience. As I was doing this, @janschulz jumped in to add below-plot annotations to ggplot2 (which we’re calling the caption label thanks to a suggestion by @arnicas).

What’s Changed?

There are two new plot label components. The first is for subtitles that appear below the plot title. You can either do:

ggtitle("The Main Title", subtitle="A well-crafted subtitle")

or

labs(title="The Main Title", subtitle="A well-crafted subtitle")

The second is for below-plot annotations (captions), which are added via:

labs(title="Main Title", caption="Where this crazy thing game from")

These are styled via two new theme elements (both adjusted with element_text() ):

plot.subtitle

plot.caption

A “casualty” of these changes is that the main plot.title is now left-justified by default (as is plot.subtitle ). plot.caption is right-justified by default.

Yet-another ggplot2 Example

I have thoughts on plot typography which I’ll save for another post, but I wanted to show how to use these new components. You’ll need to devtools::install_github("hadley/ggplot2") to use them until the changes get into CRAN.

I came across this chart from the Pew Research Center on U.S. Supreme Court “wait times” this week:

It seemed like a good candidate to test out the new ggplot2 additions. However, while Pew provided the chart, they did not provide data behind it. So, just for you, I used WebPlotDigitizer to encode the points (making good use of a commuter train home). Some points are (no doubt) off by one or two, but precision was not necessary for this riff. The data (and code) are in this gist. First the data.

library(ggplot2) dat

Now, we want to reproduce the original chart “theme” pretty closely, so I’ve done quite a bit of styling outside of the subtitle/caption. One thing we can take care of right away is how to only label every other tick:

xlabs

Now we setup the caption. It’s long, so we need to wrap it (you need to play with the number of characters value to suit your needs). There’s a Shiny Gadget (which is moving to the ggThemeAssist package) to help with this.

caption

We’re going to try to fully reproduce all the annotations, so here are the in-plot point labels. (Adding the lines is an exercise left to the reader.)

annot 841 days after the death of Henry Baldwin 1969|440|1|Henry Blackmun was sworn

in June 9, 1970, 391 days

after Abe Fortas resigned. 1990|290|0|Anthony Kennedy

was sworn in Feb.

18, 1988, 237

days after Lewis

Powell retired.", sep="|", header=TRUE, stringsAsFactors=FALSE) annot$text ", "n", annot$text)

Now the fun begins.

gg

We’ll add the y-axis “title” to the inside of the plot:

gg

Now, we add our lovingly hand-crafted in-plot annotations:

gg

Then, tweak the axes:

gg

Thanks to Hadley’s package design & Jan’s & my additions, this is all you need to do to add the subtitle & caption:

gg

Well, perhaps not all since we need to style this puppy. You’ll either need to install the font from Google Fonts or sub out the fonts for something you have.

gg

That generates:

All the annotations go with the code. No more tricks, hacks or desperate calls for help on StackOverflow!

Now, this does add two new elements to the underlying gtable that gets built, so some other StackOverflow (et al) hacks may break if they don’t use names (these elements are named in the gtable just like their ggplot2 names). We didn’t muck with the widths/columns at all, so all those hacks (mostly for multi-plot alignment) should still work.

All the code/data is (again) in this gist.