首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ggplot2中带有科学名称的多行图副标题

ggplot2中带有科学名称的多行图副标题
EN

Stack Overflow用户
提问于 2018-01-29 06:56:54
回答 1查看 1K关注 0票数 1

我在为我的ggplot2图制作字幕时遇到了困难。我试过答案here,但只有当你有一个科学名称时,它才适用。在我的例子中,我有两个科学的名字,我想包括在情节标题中。

这是我想包括的副标题:

“大量的子弹金枪鱼(Auxis rochei)和海盗凤尾鱼(Encrasicholina punctifer)的数量是由于2016年登陆量的增加所致。”

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

我的初始代码(一行):

代码语言:javascript
运行
复制
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."))

我试过:

代码语言:javascript
运行
复制
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)中。我想让它与主情节标题一样对齐。

注意:我在主情节标题(称为title in ggplot2 labs function)方面没有问题。只在subtitle。此外,我还有一个单独的主标题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-29 11:00:00

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

代码语言:javascript
运行
复制
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()

代码语言:javascript
运行
复制
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"))

希望这有帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48495915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档