154

My categories need to be named with Greek letters. I am using ggplot2, and it works beautifully with the data. Unfortunately I cannot figure out how to put those greek symbols on the x axis (at the tick marks) and also make them appear in the legend. Is there any way to do it?

UPDATE: I had a look at the link, however, there is no good method described to accomplish what I want to do.

Aristos
  • 66,005
  • 16
  • 114
  • 150
Sam
  • 7,922
  • 16
  • 47
  • 62
  • 4
    See the discussion of the `expression` function here: http://stackoverflow.com/questions/1395105/getting-latex-into-r-plots – DrewConway Mar 14 '11 at 02:43
  • Is there any hint on obtaining the `viewports` from a ggplot2. If that can be done, I believe that changing x-tic marks will be straight forward. – Sam Mar 14 '11 at 20:42
  • You can use `latex2exp` package: https://cran.r-project.org/web/packages/latex2exp/vignettes/latex2exp.html – Enrique Pérez Herrero Sep 12 '15 at 17:31

4 Answers4

175

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.

joran
  • 169,992
  • 32
  • 429
  • 468
Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • I want those Greek Symbols to mark the tics. I am not sure I follow this answer. I will try this and write back. Thank you for the direction. – Sam Mar 14 '11 at 19:57
  • This doesnt work for my purpose at least. Thank you for the pointer though. – Sam Mar 14 '11 at 20:48
  • @Sam, check out the example code in my edit. It gives you greek symbols along your x-axis tick marks. Is this what you were looking for? – Ramnath Mar 20 '11 at 19:39
  • 9
    I would like to add an asterisk ('*') after a Greek letter. Anyone know how to do this? – polarise Nov 01 '13 at 10:47
  • 2
    I recently learned that another option is to use `substitute`, this also works in places where `expression` does not work, and even allows other formatting like `italics`, `bold` etc. – Sam Apr 01 '16 at 14:13
99

Simplest solution: Use Unicode Characters

No expression or other packages needed.
Not sure if this is a newer feature for ggplot, but it works. It also makes it easy to mix Greek and regular text (like adding '*' to the ticks)

Just use unicode characters within the text string. seems to work well for all options I can think of. Edit: previously it did not work in facet labels. This has apparently been fixed at some point.

library(ggplot2)
ggplot(mtcars, 
       aes(mpg, disp, color=factor(gear))) + 
  geom_point() + 
  labs(title="Title (\u03b1 \u03a9)", # works fine
       x= "\u03b1 \u03a9 x-axis title",    # works fine
       y= "\u03b1 \u03a9 y-axis title",    # works fine
       color="\u03b1 \u03a9 Groups:") +  # works fine
  scale_x_continuous(breaks = seq(10, 35, 5), 
                     labels = paste0(seq(10, 35, 5), "\u03a9*")) + # works fine; to label the ticks
  ggrepel::geom_text_repel(aes(label = paste(rownames(mtcars), "\u03a9*")), size =3) + # works fine 
  facet_grid(~paste0(gear, " Gears \u03a9"))

Created on 2019-08-28 by the reprex package (v0.3.0)

Matt L.
  • 2,753
  • 13
  • 22
  • 8
    Nice answer. For more on unicode characters, see: https://stackoverflow.com/questions/27690729/greek-letters-symbols-and-line-breaks-inside-a-ggplot-legend-label – PatrickT Dec 07 '18 at 10:06
  • very flexible!! – joaoal Aug 23 '19 at 15:30
  • 2
    This is the best way because you don't faff about with the object types formed by `expression` or `bquote` - you end up with a character object that you can use in any other way you use character objects. – GMSL Nov 06 '19 at 10:33
  • 2
    This is not only the easiest Method, but also the best, since it works in most cases and doesn't rely on other functions. – Squeezie Jan 16 '20 at 09:21
  • This generates a lot of warnings, about a hundred lines of: `Warning message in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : “conversion failure on '>3σ' in 'mbcsToSbcs': dot substituted for ` – Patafikss Feb 05 '20 at 11:39
  • Sorry @Patafikss, I cannot reproduce any error messages. I would guess it may be a system issue. you may try it wit omitting the `ggrepel` line which introduces another package. – Matt L. Feb 05 '20 at 14:53
  • 1
    I solved the issue. I needed to use `device = cairo_pdf` in the ggsave(). Using Linux here, it's apparently a problem of the default device. – Patafikss Feb 06 '20 at 11:18
55

Use expression(delta) where 'delta' for lowercase δ and 'Delta' to get capital Δ.

Here's full list of Greek characters:

Α α alpha
Β β beta
Γ γ gamma
Δ δ delta
Ε ε epsilon
Ζ ζ zeta
Η η eta
Θ θ theta
Ι ι iota
Κ κ kappa
Λ λ lambda
Μ μ mu
Ν ν nu
Ξ ξ xi
Ο ο omicron
Π π pi
Ρ ρ rho
Σ σ sigma
Τ τ tau
Υ υ upsilon
Φ φ phi
Χ χ chi
Ψ ψ psi
Ω ω omega

EDIT: Copied from comments, when using in conjunction with other words use like: expression(Delta*"price")

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • 1
    How can I get a label like `Δprice`? `expression(Deltaprice)` doesn't work, neither does `expression(Delta price)` – jf328 Mar 29 '17 at 15:32
  • 2
    thanks. But how's that exactly? `xlab(expression(Delta)price)` gives an error – jf328 Mar 30 '17 at 10:03
  • 3
    expression(Delta*price) – kennyB Apr 08 '17 at 23:15
  • Is there a way to get a Greek-form (as opposed to Latin-form) Upsilon? See https://en.wikipedia.org/wiki/Upsilon Correspondence with Latin Y (I'd like something like the third character in https://en.wikipedia.org/wiki/Upsilon#/media/File:Y-like_European_letters.svg) – Adrian May 08 '17 at 10:09
  • 8
    expression(Delta*"price") works. Don't love the syntax. – daknowles Aug 31 '17 at 01:39
23

You do not need the latex2exp package to do what you wanted to do. The following code would do the trick.

ggplot(smr, aes(Fuel.Rate, Eng.Speed.Ave., color=Eng.Speed.Max.)) + 
  geom_point() + 
  labs(title=expression("Fuel Efficiency"~(alpha*Omega)), 
color=expression(alpha*Omega), x=expression(Delta~price))

enter image description here

Also, some comments (unanswered as of this point) asked about putting an asterisk (*) after a Greek letter. expression(alpha~"*") works, so I suggest giving it a try.

More comments asked about getting Δ Price and I find the most straightforward way to achieve that is expression(Delta~price)). If you need to add something before the Greek letter, you can also do this: expression(Indicative~Delta~price) which gets you:

enter image description here

onlyphantom
  • 8,606
  • 4
  • 44
  • 58