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 \n
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, "\n", label2.line2),
paste0(label3.line1, "\n", 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 = "\n")
, 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:
Best Answer
Baptiste's suggestion was spot on: unicode go a long way. Replace every instance of gamma above with
\u03B3
and it will display as Greek. Amazing!Printing can be done with the
cairo
device (ref):Also works:
ggsave("greek.pdf", device = cairo_pdf)
Here is a selection of unicode symbols I have found useful:
Sadly, not all special characters have a unicode equivalent. For instance, there is a unicode for y-hat but not for x-hat. It is possible to combine the letter x with a unicode bar, as in
"x\u0305" = x̅
or"p\u0302" = p̂
, but that doesn't seem to print very well.I have also had difficulty printing greek unicode as axis labels, so in these cases I resorted to
annotate()
with the optioncoord_cartesian(clip = "off")
to ensure that the labels printed below the axes do not get clipped ("on" is the default clipping behaviour).Sometimes you can directly use the character, for instance:
When unicode fails, you can try
expression()
, e.g.x̄
:For convenience, I copy some commonly used greek letters' unicodes below. Many more unicodes may be found, e.g. http://en.wikipedia.org/wiki/List_of_Unicode_characters
Information about which fonts to use with unicode: https://en.wikipedia.org/wiki/List_of_typefaces#Unicode_fonts
Some currency symbols: