I am trying to
- have line breaks (automatic or forced)
- justify the text (left or both left and right)
- have greek letters and percentage signs
inside a gglot legend label.
I have experimented with several methods, but I don't seem to be able to combine all the tricks I read about.
I can get linebreaks by inserting
into the labels, but that doesn't seem to work with greek letters, not inside legend labels. Or I could have linebreaks and greek letters in a base plot by combining mtext()
and bquote()
to insert a piece of text into the plot, (EDIT) but as pointed out by Gregor in the comments section, this doesn't work with ggplot
. Below I combine a list
with paste0()
to pass text to the legend labels: the problem is I can't find the way to insert Greek letters, e.g. gamma.
Grateful for suggestions.
Here is a MWE with one of my attempts (Edit: improved MWE):
label1.line1 <- "Not much to say about this one"
label2.line1 <- "blabla blabla, blabla blabla, blee blee blee (bling, bling, bling),"
label2.line2 <- paste0("(", "1900-2014: g = 1.50%, gamma = 2.75%, and r = 2.30%",")")
label3.line1 <- "blabla blabla, blabla blabla, blee blee blee (bling, bling, bling),"
label3.line2 <- paste0("(", "1900-2014: g = 2.50%, gamma = 1.75%, and r = 2.30%",")")
labels_fixed <- list(
label1.line1,
paste0(label2.line1, "
", label2.line2),
paste0(label3.line1, "
", label3.line2)
)
library(ggplot2)
library(scales)
library(grid) # provides unit() function used to tweak spacing inside legend
ggplot(data = mtcars, aes(x = mpg, y = wt, group = factor(cyl), colour = factor(cyl), shape = factor(cyl))) +
geom_line() + geom_point(size = 3) + theme_bw() +
scale_shape_manual(name = "Details", values = c(17, 21, 15),
labels = labels_fixed) +
scale_colour_manual(name = "Details", values = c("darkred", "darkgreen", "darkblue"),
labels = labels_fixed) +
theme(legend.key = element_blank(),
legend.position = c(.65, .8),
legend.background = element_rect(colour = "black"),
legend.key.size = unit(2, "lines"),
legend.text = element_text(size = 15))
A minor problem is that the legend text is much smaller with multiple lines so the legend line spacing will need to be tweaked: I was able to tweak legend.key.size
and legend.text
to achieve a better result than the default, which is too cramped.
I also tried another suggestion involving cat(strwrap("long label here"), sep = "
")
, but I couldn't get that to work. I also tried atop
but that makes each line much too small and the nesting required to achieve the desired stacking is tedious.
Is there any way to get Greek letters?
Here are some useful suggestions I couldn't make to work, e.g. the combination of mtext()
and bquote()
is mentioned in 1 and 2:
- Expression and new line in plot labels
- Line break in expression()?
- Wrap horizontal legend across multiple rows
- using expression(paste( to insert math notation into a ggplot legend
- ggplot2 two-line label with expression
- How to annotate() ggplot with latex
- Greek letters in ggplot annotate
See Question&Answers more detail:
os