我在正确对齐包含数学表达式的多行图例标题时遇到困难:
# Simulate example data and create plot
df <- data.frame(x = 1:3, y = 1:3, z = 1:3)
gg <- ggplot(df, aes(x, y, colour = z)) + geom_point()
# Legend title that is causing problems:
expectation <- bquote(paste("E(\U03BC | ", bold(Z), ", ", bold("\U03B8"), ")"))
legend_title <- bquote(paste("Prediction\n", .(expectation), sep = ""))
gg <- gg + labs(colour = legend_title)
gg
我希望“预测”以“E(mu | Z | theta)”为中心(或者,让它们都左对齐)。我尝试过使用 theme()
和 guides()
,但无济于事:
# These approaches do not fix the problem:
gg + theme(legend.title.align = 0.5)
gg + guides(colour = guide_legend(title.hjust = 0.5))
最佳答案
使用atop
有效。
ggplot(df, aes(x, y, colour = z)) + geom_point() +
labs(colour = bquote(atop("Prediction" ,paste("E(\U03BC | ", bold(Z), ", ", bold("\U03B8"), ")")))
)
关于r - 将多行图例标题与数学表达式对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69127037/