ggplot2标题中的多行`表达式()`

时间:2016-07-28 06:48:47

标签: r ggplot2

我在\n轴标签中使用expressionggplot2阅读此question和另一个question回合,但似乎它对标题不起作用(或多行表达式)。

我原来的情节如下:

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="")))

enter image description here

但标题太长,所以我尝试使用引用问题中建议的atop命令放置两个断行:一个在 vs 模型之间的第二个(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 个答案:

答案 0 :(得分:0)

您需要增加上边距。然后,表达式不适合换行符。试试这个:

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()中的粘贴命令。您可以按","在表达式中设置新行。