Corner Labels in ggplot2

前端 未结 4 1784
后悔当初
后悔当初 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 11:20

    Here's a solution using a custom labeller function. This doesn't invovle any manipulations to the data. Currently it only works with 1-dimensional facets (facet_wrap). I'm still working on how to increment along a 2-D grid...

    1. Define the labeller function

      make_labelstring <- function(mypanels) {
        mylabels <- sapply(mypanels, 
                           function(x) {LETTERS[which(mypanels == x)]})
      
        return(mylabels)
      }
      
      label_panels <- ggplot2::as_labeller(make_labelstring)
      
    2. Pass label_panels as the labeller to facet_wrap

      library(ggplot2)
      data("diamonds")
      
      # create a faceted plot
      ggplot(data = diamonds, aes(x = depth, y = price)) +
        geom_point() +
        facet_wrap(~cut, labeller = label_panels) +
        theme(strip.text = element_text(hjust = -0),
              strip.background = element_blank())
      

提交回复
热议问题