【问题标题】:Multi line plot subtitle with scientific names in ggplot2ggplot2中带有学名的多线图字幕
【发布时间】:2018-07-07 19:59:34
【问题描述】:

我无法为我的 ggplot2 图表制作字幕。我尝试了答案here,但它仅适用于您有一个学名的情况。就我而言,我想在情节标题中包含两个学名。

这是我想要包含的字幕:

“子弹头金枪鱼 (Auxis rochei) 和海盗鳀鱼 (Encrasicholina punctifer) 的高产量归因于 2016 年上岸量的增加。”

我希望它是多行的,因为它很长。

我的初始代码(一行):

mysubtitle <- expression(paste("High quantity of Bullet tuna ", italic("Auxis rochei"), " and ", "Buccaneer anchovy ", italic("Encrasicholina punctifer"), " were attributed to the increase in the landings in 2016."))

我试过了:

mysubtitle <- expression(atop(paste("High quantity of Bullet tuna (", italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", italic("Encrasicholina punctifer"), ")"), paste("were attributed to the increase in the landings in 2016.")))

上面的代码生成了一个两行标题,但它是居中的,尽管在我的 ggplot2 主题plot.subtitle = element_text(hjust = 0) 中。我希望它与主情节标题保持一致。

注意:我在主情节标题上没有问题(在 ggplot2 labs 函数中称为title)。仅在subtitle 中。另外,我还有一个单独的主标题。

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    从“它很愚蠢但它有效”中,您可以在第二行的右侧添加支架字符串以强制左对齐。权利持有者字符串可以是任意字符,长度为nchar(first_line - nchar(second_line))

    library(ggplot2)
    first_line<- "High quantity of Bullet tuna (Auxis rochei) and Buccaneer anchovy (Encrasicholina punctifer)"
    second_line <- "were attributed to the increase in the landings in 2016."
    # the length of holder_string
    holder_length <- nchar(first_line) - nchar(second_line)
    # generate a arbitrary string with length `holder_length`
    holder_string <- stringi::stri_rand_strings(1, holder_length)
    # the default font family of ggplot is `sans`, we should set the font family as `mono` (monospaced font) to ensure strings between two lines aligned. 
    p  <- ggplot(mtcars, aes(wt, mpg))+ geom_point()
    p + labs(subtitle = bquote(atop(
      paste("High quantity of Bullet tuna (", 
        italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", 
        italic("Encrasicholina punctifer"), ")"), 
      paste("were attributed to the increase in the landings in 2016.", 
        phantom(.(holder_string))
      )))
    ) +
    theme(plot.subtitle = element_text(family = "mono"))
    

    编辑

    或者,你可以使用substitute()

    p + labs(subtitle = substitute(atop(
      paste("High quantity of Bullet tuna (", 
        italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", 
        italic("Encrasicholina punctifer"), ")"), 
      paste("were attributed to the increase in the landings in 2016.", 
        phantom(A))
      ),list(A = holder_string))) + 
    theme(plot.subtitle = element_text(family = "mono"))
    

    希望对你有帮助!

    【讨论】:

    • 这有帮助。您能否就您的答案提出参考建议,以便我阅读它以进一步加强我的学习?
    • 我们可以将字体系列设置为mono,以确保字母和间距具有相同的宽度。然后可以生成长度为nchar(first_line) - nchar(second_line)的任意字符作为持有者字符。
    • phantom() 是 plotmath 中的一个功能,可用于创建隐形角色。 This post 展示了如何在绘图表达式中使用变量(就像在上面的答案中如何使用 holder_string
    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多