Corner Labels in ggplot2

Thraupidae picture Thraupidae · Jul 10, 2013 · Viewed 10.5k times · Source

I'm interested in trying to create simple corner labels for a multipanel figure I am preparing in ggplot. This is similar to this previously asked question, but the answers only explained how to include a label at the top of the plot, not produce a corner label in the format required by many journals. I hope to replicate something similar to the plotrix function corner.label() in ggplot2.

Here is an example using plottrix of what I would like to recreate in ggplot2.

require(plotrix)

foo1<-rnorm(50,25,5)
foo2<-rpois(50,25)
foo3<-rbinom(50,25,0.5)
foo4<-rnbinom(50,25,0.5)

par(mfrow=c(2,2))
hist(foo1)
corner.label(label='a',figcorner=T)
hist(foo2)
corner.label(label='b',figcorner=T)
hist(foo3)
corner.label(label='c',figcorner=T)
hist(foo4)
corner.label(label='d',figcorner=T)

This produces the following:

enter image description here

Thanks for any help in advance!

Answer

Michael Harper picture Michael Harper · Jul 16, 2018

Two recent changes have made this a lot easier:

  • The latest release of ggplot2 has added the tag caption which can be used to label subplots.
  • The package patchwork makes it really easy to plot multiple ggplot objects. https://github.com/thomasp85/patchwork

This means that no altering of grobs is required. Adapting the reproducible example provided by Kev:

library(ggplot2)
# install.package("patchwork")
library(patchwork)

a <- 1:20
b <- sample(a, 20)
c <- sample(b, 20)
d <- sample(c, 20)
mydata   <- data.frame(a, b, c, d)

myplot1  <- ggplot(mydata, aes(x=a, y=b)) + geom_point() + labs(tag = "A")
myplot2  <- ggplot(mydata, aes(x=b, y=c)) + geom_point() + labs(tag = "B")
myplot3  <- ggplot(mydata, aes(x=c, y=d)) + geom_point() + labs(tag = "C")
myplot4  <- ggplot(mydata, aes(x=d, y=a)) + geom_point() + labs(tag = "D")

myplot1 + myplot2 + myplot3 + myplot4

enter image description here