Multi line title in ggplot 2 with multiple italicized words

后端 未结 3 555
闹比i
闹比i 2020-12-10 04:59

I am trying to create a plot title manually formatted on two lines which includes two italicized words, I have done some s

3条回答
  •  佛祖请我去吃肉
    2020-12-10 05:13

    As an alternative to inserting line breaks in title, you may use title together with subtitle (available from ggplot 2.2.0). Possibly this makes the plothmathing slightly more straightforward.

    p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
      geom_point() +
      labs(title = expression("First line: "*italic("Honorificabilitudinitatibus")),
           subtitle = expression("Second line: "*italic("Honorificabilitudinitatibus praelongus")*" and more"))
    p
    


    If you wish the font size to be the same on both lines, set the desired size in theme.

    p + theme(plot.title = element_text(size = 12),
              plot.subtitle = element_text(size = 12))
    

    Note that both title and subtitle are left-aligned by default in ggplot2 2.2.0. The text can be centered by adding hjust = 0.5 to element_text above.

提交回复
热议问题