ggplot2标题中的多行`expression()`

5

我正在阅读这个问题和这个问题,涉及在ggplot2轴标签中使用\nexpression,但似乎对于标题(或多行表达式)不起作用。

我的原始图如下:

ggplot(data = fig3, aes(x=crude_beta_time6, y=vidD_beta_time6)) + 
    geom_point(shape=18, size=5, color="gray60") + xlab("Coefficients of the crude model") + 
    ylab(bquote("Coefficients of the total 25(OH)D"[3]~"adjusted model")) + 
    theme_bw(base_size = 17) + theme(
        panel.grid.major = element_line(color = "gray20", size = 0.3, linetype = "dashed"),
        panel.grid.minor = element_line(color = "gray40", size = 0.3, linetype = "dashed")
    ) + ggtitle(expression(paste("(B) Coefficients of the crude model vs the total 25(OH)", D[3]," adjusted model (0h vs 6h)", sep="")))

图片描述在这里输入

然而,标题太长了,因此我尝试使用引用的问题中推荐的atop命令来放置两个换行符:一个在vsthe之间,另一个在model(0h vs 6h)之间。

ggplot(data = fig3, aes(x=crude_beta_time6, y=vidD_beta_time6)) + 
    geom_point(shape=18, size=5, color="gray60") + xlab("Coefficients of the crude model") + 
    ylab(bquote("Coefficients of the total 25(OH)D"[3]~"adjusted model")) + 
    theme_bw(base_size = 17) + theme(
        panel.grid.major = element_line(color = "gray20", size = 0.3, linetype = "dashed"),
        panel.grid.minor = element_line(color = "gray40", size = 0.3, linetype = "dashed")
    ) + ggtitle(expression(atop(paste("(B) Coefficients of the crude model vs\nthe total 25(OH)", D[3]," adjusted model\n(0h vs 6h)", sep=""))))

我遇到了标题的奇怪行为:

enter image description here

如何通过表达式获得一个三行居中的标题?

更新:

用户Shirin Glander建议更新图形的边距以获得更多标题空间。提供的代码如下:

ggplot(data = fig3, aes(x=crude_beta_time6, y=vidD_beta_time6)) + 
geom_point(shape=18, size=5, color="gray60") + xlab("Coefficients of the crude model") + 
ylab(bquote("Coefficients of the total 25(OH)D"[3]~"adjusted model")) + 
theme_bw(base_size = 17) + theme(
    panel.grid.major = element_line(color = "gray20", size = 0.3, linetype = "dashed"),
    panel.grid.minor = element_line(color = "gray40", size = 0.3, linetype = "dashed")) + 
ggtitle(expression(atop(paste("(B) Coefficients of the crude model vs\nthe total 25(OH)", D[3]," adjusted model\n(0h vs 6h)", sep="")))) + 
theme(plot.margin=unit(c(4,0,0,0),"cm"))

这段代码的结果为标题添加了更多空间,但是标题没有正确显示:

enter image description here

1个回答

2
你需要增加上边距。此外,表达式不能很好地处理换行符。请尝试这样做:
ggplot(data = fig3, aes(x=crude_beta_time6, y=vidD_beta_time6)) + 
geom_point(shape=18, size=5, color="gray60") + xlab("Coefficients of the crude model") + 
ylab(bquote("Coefficients of the total 25(OH)D"[3]~"adjusted model")) + 
theme_bw(base_size = 17) + theme(
    panel.grid.major = element_line(color = "gray20", size = 0.3, linetype = "dashed"),
    panel.grid.minor = element_line(color = "gray40", size = 0.3, linetype = "dashed")
) + ggtitle(expression(atop("(B) Coefficients of the crude model vs", "the total 25(OH)" ~ D[3] ~ "adjusted model (0h vs 6h)"))) + 
theme(plot.margin=unit(c(4,0,0,0),"cm"))

我只是添加了theme(plot.margin=unit(c(4,0,0,0),"cm"))并且移除了在expression()内的粘贴命令。你可以通过使用","在表达式中设置新行。


谢谢你的回答,但对我来说没有用。我已经更新了问题,并附上了另一张获得结果的图片。虽然边距或多或少地设置正确,但标题显示不正确。 - carlesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接