Search code examples
rggplot2labelpanel

Label individual panels in a multi-panel ggplot2


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!


Solution

  • I had the same problem and came up with the following solution, which is a bit different:

    loading r packages

    library(ggplot2)
    library(grid)
    library(gridExtra)
    

    example data

    a <- 1:20
    b <- sample(a, 20)
    c <- sample(b, 20)
    d <- sample(c, 20)
    

    create a data frame

    mydata   <- data.frame(a, b, c, d)
    

    create example plots

    myplot1  <- ggplot(mydata, aes(x=a, y=b)) + geom_point()
    myplot2  <- ggplot(mydata, aes(x=b, y=c)) + geom_point()
    myplot3  <- ggplot(mydata, aes(x=c, y=d)) + geom_point()
    myplot4  <- ggplot(mydata, aes(x=d, y=a)) + geom_point()
    

    set corner labels

    myplot1 <- arrangeGrob(myplot1, top = textGrob("A", x = unit(0, "npc")
             , y   = unit(1, "npc"), just=c("left","top"),
             gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))
    
    myplot2 <- arrangeGrob(myplot2, top = textGrob("B", x = unit(0, "npc")
             , y = unit(1, "npc"), just=c("left","top"),
             gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))
    
    myplot3 <- arrangeGrob(myplot3, top = textGrob("C", x = unit(0, "npc")
            , y  = unit(1, "npc"), just=c("left","top"),
            gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))
    
    myplot4 <- arrangeGrob(myplot4, top = textGrob("D", x = unit(0, "npc")
            , y = unit(1, "npc"), just=c("left","top"),
            gp=gpar(col="black",    fontsize=18, fontfamily="Times Roman")))
    

    plotting all plots on one page

    grid.arrange(myplot1, myplot2, myplot3, myplot4, ncol = 2)
    

    corner label