我在\n
轴标签中使用expression
和ggplot2
阅读此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="")))
但标题太长,所以我尝试使用引用问题中建议的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=""))))
我得到了这个标题的奇怪行为:
如何让三行和居中的标题成为一种表达方式?
更新:
用户 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"))
此代码的结果为标题添加了更多空间,但标题未显示属性:
答案 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()
中的粘贴命令。您可以按","
在表达式中设置新行。