我在为我的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)
中。我想让它与主情节标题一样对齐。
注意:我在主情节标题(称为title
in ggplot2 labs
function)方面没有问题。只在subtitle
。此外,我还有一个单独的主标题。
发布于 2018-01-29 11:00:00
从“这很愚蠢,但它有效”,您可以添加持有人字符串的右边第二行,以强制左对齐。右持有者字符串可以是任意字符,其长度为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"))
希望这有帮助!
https://stackoverflow.com/questions/48495915
复制相似问题