gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:59:10 24 4
gpt4 key购买 nike

我正在读这个 question和这个其他 question关于使用 \nexpressionggplot2轴标签,但它似乎不适用于标题(或多行表达式)。

我原来的情节如下:

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 和 the 之间的一个,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

最佳答案

您需要增加上边距。然后,表达式在换行时也不能很好地发挥作用。尝试这个:

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

关于r - ggplot2 标题中的多行 `expression()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38628969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com