Left justify text from multi-line facet labels

后端 未结 3 1358
温柔的废话
温柔的废话 2021-01-03 22:57

I\'m using facet_grid() to display some data, and I have facet labels that span multiple lines of text (they contain the "\\n" character).

require(g         


        
3条回答
  •  天命终不由人
    2021-01-03 23:15

    There may be a cleaner way to do this but I didn't find a way to do this within ggplot2. The padwrap function could be more generalized as it basically does just what you requested. To get the justification right, I had to use a mono-spaced font.

    # Wrap text with embedded newlines: space padded and lef justified.
    # There may be a cleaner way to do this but this works on the one
    # example.  If using for ggplot2 plots, make the font `family`
    # a monospaced font (e.g. 'Courier')
    padwrap <- function(x) {
        # Operates on one string
        padwrap_str <- function(s) {
            sres    <- strsplit(s, "\n")
            max_len <- max(nchar(sres[[1]]))
            paste( sprintf(paste0('%-', max_len, 's'), sres[[1]]), collapse = "\n" )
        }
        # Applys 'padwrap' to a vector of strings
        unlist(lapply(x, padwrap_str))
    }
    
    require(ggplot2)
    
    facet_label_text = rep(c("Label A",
                             "Label B\nvery long label",
                             "Label C\nshort",
                             "Label D"), 5)
    new_facet_label_text <- padwrap(facet_label_text)
    
    #Generate example data
    set.seed(3)
    df = data.frame(facet_label_text = new_facet_label_text,
                    time = rep(c(0, 4, 8, 12, 16), times = 4),
                    value = runif(20, min=0, max=100))
    
    #Plot test data
    ggplot(df, aes(x = time, y = value)) +
        geom_line() +
        facet_grid(facet_label_text ~ .) +
        theme(strip.text.y = element_text(angle = 0, hjust = 0, family = 'Courier'))
    

    The strip text is left justified in the image below

提交回复
热议问题