Left justify text from multi-line facet labels

后端 未结 3 1334
温柔的废话
温柔的废话 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:32

    Until someone comes along with a real solution, here's a hack: Add space in the labels to get the justification you want.

    require(ggplot2)
    
    #Generate example data
    set.seed(3)
    df = data.frame(facet_label_text = rep(c("Label A",
                                             "Label B           \nvery long label",
                                             "Label C\nshort     ",
                                             "Label D"),
                                           each = 5),
                    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))
    

提交回复
热议问题