Corner Labels in ggplot2

前端 未结 4 1754
后悔当初
后悔当初 2020-12-29 10:31

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 11:15

    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
    

提交回复
热议问题