Home > OS >  ggplot2: Aligning multi-line legend titles with math expressions
ggplot2: Aligning multi-line legend titles with math expressions

Time:09-17

I am having difficulty properly aligning a multi-line legend title that contains math expressions:

# 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

lines

CodePudding user response:

I can't comment, but the issue is that expressions default to right justification. See this post for a solution: ggplot right align axis text when using expression

  • Related