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.
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)
Use expression(delta)
where 'delta' for lowercase d
and 'Delta' to get capital ?
.
Here's full list of Greek characters:
? a alpha
? ß beta
G ? gamma
? d delta
? e epsilon
? ? zeta
? ? eta
T ? theta
? ? iota
? ? kappa
? ? lambda
? µ mu
? ? nu
? ? xi
? ? omicron
? p pi
? ? rho
S s sigma
? t tau
? ? upsilon
F f phi
? ? chi
? ? psi
O ? omega
EDIT: Copied from comments, when using in conjunction with other words use like: expression(Delta*"price")
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))
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:
Source: Stackoverflow.com