Greek Letters, Symbols, and Line Breaks Inside a Ggplot Legend Label

Greek letters, symbols, and line breaks inside a ggplot legend label

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):

library(Cairo)
cairo_pdf(file = "ggplot-greek.pdf", width = 8, height = 5)
## ggplot object created here
dev.off()

Also works: ggsave("greek.pdf", device = cairo_pdf)

Here is a selection of unicode symbols I have found useful:

"\U016B" = ū
"\U016A" = Ū
"\U00FB" = û
"\U00DB" = Û
"\U0233" = ȳ
"\U0232" = Ȳ
"\U0177" = ŷ
"\U0176" = Ŷ
"\U0113" = ē
"\U0112" = Ē
"\u00EA" = ê
"\U00CA" = Ê
"\U003BC\U2080" = μ₀
"\U003BC\U2081" = μ₁
"R\U00B2" = R²
"\u221E" = ∞
"\u2248" = ≈
"\U2260" = ≠
"\u03C3/\u221An" = σ/√n

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 option coord_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:

myplot + labs(title  = "±")

When unicode fails, you can try expression(), e.g. :

title = expression(paste("z = (", bar(x), "-\u03BC)/(\u03C3/\u221An)", sep = ""))  
library(ggplot2)
ggplot() + labs(title = title) + theme_bw()

title

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

\u0391  Α   Greek Capital Letter Alpha
\u0392 Β Greek Capital Letter Beta
\u0393 Γ Greek Capital Letter Gamma
\u0394 Δ Greek Capital Letter Delta
\u0395 Ε Greek Capital Letter Epsilon
\u0396 Ζ Greek Capital Letter Zeta
\u0397 Η Greek Capital Letter Eta
\u0398 Θ Greek Capital Letter Theta
\u0399 Ι Greek Capital Letter Iota
\u039A Κ Greek Capital Letter Kappa
\u039B Λ Greek Capital Letter Lambda
\u039C Μ Greek Capital Letter Mu
\u039D Ν Greek Capital Letter Nu
\u039E Ξ Greek Capital Letter Xi
\u039F Ο Greek Capital Letter Omicron
\u03A0 Π Greek Capital Letter Pi
\u03A1 Ρ Greek Capital Letter Rho
\u03A3 Σ Greek Capital Letter Sigma
\u03A4 Τ Greek Capital Letter Tau
\u03A5 Υ Greek Capital Letter Upsilon
\u03A6 Φ Greek Capital Letter Phi
\u03A7 Χ Greek Capital Letter Chi
\u03A8 Ψ Greek Capital Letter Psi
\u03A9 Ω Greek Capital Letter Omega
\u03B1 α Greek Small Letter alpha
\u03B2 β Greek Small Letter beta
\u03B3 γ Greek Small Letter gamma
\u03B4 δ Greek Small Letter delta
\u03B5 ε Greek Small Letter epsilon
\u03B6 ζ Greek Small Letter zeta
\u03B7 η Greek Small Letter eta
\u03B8 θ Greek Small Letter theta
\u03B9 ι Greek Small Letter iota
\u03BA κ Greek Small Letter kappa
\u03BB λ Greek Small Letter lambda
\u03BC μ Greek Small Letter mu
\u03BD ν Greek Small Letter nu
\u03BE ξ Greek Small Letter xi
\u03BF ο Greek Small Letter omicron
\u03C0 π Greek Small Letter pi
\u03C1 ρ Greek Small Letter rho
\u03C2 ς Greek Small Letter final sigma
\u03C3 σ Greek Small Letter sigma
\u03C4 τ Greek Small Letter tau
\u03C5 υ Greek Small Letter upsilon
\u03C6 φ Greek Small Letter phi
\u03C7 χ Greek Small Letter chi
\u03C8 ψ Greek Small Letter psi
\u03C9 ω Greek Small Letter omega

Information about which fonts to use with unicode: https://en.wikipedia.org/wiki/List_of_typefaces#Unicode_fonts

Some currency symbols:

# http://www.fileformat.info/info/unicode/category/Sc/list.htm
Character Name Browser Image
\u0024 DOLLAR SIGN $
\u00A2 CENT SIGN ¢
\u00A3 POUND SIGN £
\u00A4 CURRENCY SIGN ¤
\u00A5 YEN SIGN ¥
\u058F ARMENIAN DRAM SIGN
\u060B AFGHANI SIGN ؋
\u09F2 BENGALI RUPEE MARK ৲
\u09F3 BENGALI RUPEE SIGN ৳
\u09FB BENGALI GANDA MARK
\u0AF1 GUJARATI RUPEE SIGN ૱
\u0BF9 TAMIL RUPEE SIGN ௹
\u0E3F THAI CURRENCY SYMBOL BAHT ฿
\u17DB KHMER CURRENCY SYMBOL RIEL ៛
\u20A0 EURO-CURRENCY SIGN ₠
\u20A1 COLON SIGN ₡
\u20A2 CRUZEIRO SIGN ₢
\u20A3 FRENCH FRANC SIGN ₣
\u20A4 LIRA SIGN ₤
\u20A5 MILL SIGN ₥
\u20A6 NAIRA SIGN ₦
\u20A7 PESETA SIGN ₧
\u20A8 RUPEE SIGN ₨
\u20A9 WON SIGN ₩
\u20AA NEW SHEQEL SIGN ₪
\u20AB DONG SIGN ₫
\u20AC EURO SIGN €
\u20AD KIP SIGN ₭
\u20AE TUGRIK SIGN ₮
\u20AF DRACHMA SIGN ₯
\u20B0 GERMAN PENNY SIGN ₰
\u20B1 PESO SIGN ₱
\u20B2 GUARANI SIGN ₲
\u20B3 AUSTRAL SIGN ₳
\u20B4 HRYVNIA SIGN ₴
\u20B5 CEDI SIGN ₵
\u20B6 LIVRE TOURNOIS SIGN ₶
\u20B7 SPESMILO SIGN ₷
\u20B8 TENGE SIGN ₸
\u20B9 INDIAN RUPEE SIGN ₹
\u20BA TURKISH LIRA SIGN ₺
\u20BB NORDIC MARK SIGN ₻
\u20BC MANAT SIGN ₼
\u20BD RUBLE SIGN ₽
\uA838 NORTH INDIC RUPEE MARK
\uFDFC RIAL SIGN ﷼
\uFE69 SMALL DOLLAR SIGN ﹩
\uFF04 FULLWIDTH DOLLAR SIGN $
\uFFE0 FULLWIDTH CENT SIGN ¢
\uFFE1 FULLWIDTH POUND SIGN £
\uFFE5 FULLWIDTH YEN SIGN ¥
\uFFE6 FULLWIDTH WON SIGN

How to use Greek symbols in ggplot2?

Here is a link to an excellent wiki that explains how to put greek symbols in ggplot2. In summary, here is what you do to obtain greek symbols

  1. Text Labels: Use parse = T inside geom_text or annotate.
  2. Axis Labels: Use expression(alpha) to get greek alpha.
  3. Facet Labels: Use labeller = label_parsed inside facet.
  4. Legend Labels: Use bquote(alpha == .(value)) in legend label.

You can see detailed usage of these options in the link

EDIT. The objective of using greek symbols along the tick marks can be achieved as follows

require(ggplot2);
data(tips);
p0 = qplot(sex, data = tips, geom = 'bar');
p1 = p0 + scale_x_discrete(labels = c('Female' = expression(alpha),
'Male' = expression(beta)));
print(p1);

For complete documentation on the various symbols that are available when doing this and how to use them, see ?plotmath.

Adding Greek letters in legend of GGPLOT

Following up on some comments: the important function to set the labels is scale_fill_discrete (as suggested by @Skaqqs). But you don't need to use Unicode to get Greek letters (or math notation in general). If you set labels to expression(tau[1], tau[2]) you'll get the Greek letters with subscripts as you wanted.

In this example, you can't simplify expression(tau[1], tau[2]) by much, but in other cases you might want the subscripts to depend on data. You can do that using subscripts <- 1:2; parse(text = paste("tau[", subscripts, "]")).

Putting this all together,

library(ggplot2)
library(magrittr)
set.seed(1)
Dat = rbind(data.frame(var1 = 'x1', var2 = rnorm(10000, 10, 3)),
data.frame(var1 = 'x2', var2 = rnorm(10000, 10, 3)))

Dat %>%
ggplot() +
geom_histogram(data = Dat, aes(x = var2, y = ..density.., fill = var1)) +
scale_fill_discrete(labels = parse(text = paste("tau[", 1:2, "]")))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Sample Image

Created on 2021-10-01 by the reprex package (v2.0.0)

Adding greek symbols to legend ggplot (more than one)

Maybe this. You can smartly use paste() to concatenate the elements and add the , using expression() function:

library(ggplot2)
#Code
d <- data.frame(x=1:10,y=1:10,f=rep(c("alpha","beta"),each=5), stringsAsFactors=FALSE)
value <- 3.1415
my.labs <- c(expression(paste(alpha==1,',',~gamma==2)),expression(beta))
#Plot
qplot(x,y,data=d,colour=f) +
scale_colour_manual(values=1:2,breaks=c("alpha","beta"),
labels=my.labs)

Output:

Sample Image

ggplot greek letter in label within for loop indices

Using bquote you could do:

Note: Instead of using a for loop I switched to lapply (and would suggest to so) as sooner or later you will run in issues related to tidy evaluation when using a for loop with ggplot. And there are plenty of questions on SO related to that. (:

var <- c("a", "b", "c")

df <- data.frame(x = c(1:20), y = c(41:60))

library(ggplot2)

lapply(var, function(x) {
ggplot() +
geom_line(data = df, aes(x = x, y = y)) +
xlab(bquote(theta*.(x)))
})
#> [[1]]

Sample Image

#> 
#> [[2]]

Sample Image

#> 
#> [[3]]

Sample Image



Related Topics



Leave a reply



Submit