r - 将多行图例标题与数学表达式对齐

标签 r ggplot2

我在正确对齐包含数学表达式的多行图例标题时遇到困难:

# 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

Example plot

我希望“预测”以“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"), ")")))
  )

lines

关于r - 将多行图例标题与数学表达式对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69127037/

相关文章:

r - 如何使用ggplot2将光栅文件绘制为直方图?

r - 在每个单元格中构建具有自定义颜色的六角形热图

r - 一个 R ggplot2 图中的多个数据点

r - 使用 ggplot2 创建多点图 + 箱线图 + 线图

r - 创建一个数据框,其值列名称与另一个数据框的名称匹配

r - 如何在同一图表中将(n 未知)数量的数据系列绘制为 geom_line

r - 将组平均线添加到 geom_bar 图并包括在图例中

r - 如何将两行移动到 R 列表中数据框的顶部?

r - 将向量存储为数据框条目?

java - ls(envir = envir, all.names = private) 错误 : invalid 'envir' argument in R