【问题标题】:Multi line title in ggplot 2 with multiple italicized wordsggplot 2中的多行标题带有多个斜体字
【发布时间】:2016-11-21 12:37:49
【问题描述】:

我正在尝试创建一个情节标题手动格式化两行其中包括两个斜体字,我做了一些searching 在 Stack Exchange 上,但还没有找到解决这个看似简单的问题的好方法。

这两个物种的学名相当长,因此需要多行标题(ggplot2 没有格式化)。

目标:

........标题的第一行带有物种

第二行字anotherItalicSpecies结尾

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  labs(title= expression(paste(atop("First line of title with ", atop((italic("Species")))),"
       secondline words", italic("anotherSpecies"), "the end")))

这会产生以下损坏的标题:

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    使用atoppasteitalicscriptstyle 的组合:

    ggplot(mtcars, aes(x = wt, y = mpg)) +
      geom_point() +
      labs(title = ~ atop(paste('First line of title with ',italic("Species")),
                          paste(scriptstyle(italic("Species")),
                                scriptstyle(" secondline words "),
                                scriptstyle(italic("anotherSpecies")),
                                scriptstyle(" the end"))))
    

    给你想要的结果:

    使用scriptstyle 不是必需的,但恕我直言,让您的副标题使用比主标题更小的字体。

    另请参阅 ?plotmath 了解其他有用的自定义设置。

    【讨论】:

    • 这个解决方案能否将标题对齐到情节的左侧?
    • @user7395965 不完全;如果需要,最好使用 Henrik 的解决方案
    【解决方案2】:

    作为在title 中插入换行符的替代方法,您可以将titlesubtitle 一起使用(可从ggplot 2.2.0 获得)。可能这会使plothmathing 稍微简单一些。

    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
    


    如果您希望两行的字体大小相同,请在theme 中设置所需的size

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

    请注意,ggplot2 2.2.0 中的标题和副标题默认都是左对齐的。文字可以通过在上面添加hjust = 0.5element_text 来居中。

    【讨论】:

    • 很好,我只是想在我的回答中包含开发版本的这些改进;-)
    • 哈哈 :-) (加号)。无论如何,显然 ggplot 上周更新了,没有注意到 :-/.两种选择都很好。
    • 已升级,不错的解决方案!但现在我收到来自 geom_ribbon 中 y 美学的错误...似乎与 ggplot 2.2.0 有关。我会做一些搜索,但你有什么想法吗?
    • @Arch 如果您遇到新问题,最好在 imo 上发布新问题。
    【解决方案3】:

    我们也可以调用cowplot::draw_label() 两次(灵感来自this 讨论)。但是,我们需要稍微调整一下位置并为自定义标题留出足够的空间。我对这种方法进行了更多解释,并在ggplot2 two-line label with expression 中使用了ggplot2::annotation_custom()

    library(ggplot2)
    library(cowplot)
    #> 
    #> Attaching package: 'cowplot'
    #> The following object is masked from 'package:ggplot2':
    #> 
    #>     ggsave
    
    # The two lines we wish on the plot. The ~~ creates extra space between the
    # expression's components, might be needed here.
    line_1 <- expression("First Line of Title with" ~~ italic("Species"))
    line_2 <- expression(italic("Species") ~~ "second line words" ~~ italic("anotherSpecies") ~~ "the end")
    
    # Make enough space for the custom two lines title
    p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
      geom_point() + 
      labs(title = "") + # empty title
      # Force a wider top margin to make enough space
      theme(plot.title = element_text(size = 10, # also adjust text size if needed
                                      margin = margin(t = 10, r = 0, b = 0, l = 0,
                                                      unit = "mm")))
    
    # Call cowplot::draw_label two times to plot the two lines of text
    ggdraw(p) +
      draw_label(line_1, x = 0.55, y = 0.97) +
      draw_label(line_2, x = 0.55, y = 0.93)
    

    reprex package (v0.2.1) 于 2019 年 1 月 14 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多