1

I am trying to add Greek letters in legend of my GGPLOT. Below is my approach

library(ggplot2)
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_colour_manual(labels = expression(paste('tau[', 1:2, ']', sep = '')))

With above approach, I am getting original variables in legend.

I also tried below (as explained in How to use Greek symbols in ggplot2?)

Dat %>% 
  ggplot() +
  geom_histogram(data = Dat, aes(x = var2, y = ..density.., fill = var1)) +
  scale_x_discrete(labels = c('x1' = expression(alpha),
                                      'x2'   = expression(beta)))

But still I failed to change the legend.

How can I modify those variables names to greek letters preserving all other properties of my plot?

Brian Smith
  • 1,200
  • 4
  • 16
  • https://stackoverflow.com/questions/5293715/how-to-use-greek-symbols-in-ggplot2 – Skaqqs Oct 01 '21 at 13:19
  • Does this answer your question? [How to use Greek symbols in ggplot2?](https://stackoverflow.com/questions/5293715/how-to-use-greek-symbols-in-ggplot2) – jdobres Oct 01 '21 at 13:20
  • Unfortunately I still could not bring Greek letters in legend. I modified my original question with this finding – Brian Smith Oct 01 '21 at 13:26
  • 1
    In your first example you are trying to set "color" but are using "fill" (and no color argument). In the example where your write " *I also tried below* " you are trying to set "x_discrete" but are using a continuous variable for x... The answer you were given below as well as **ALL** the suggested links to existing answers work. It looks like your problem has nothing to do with greek letters and everything with how to use ggplot2 syntax – dario Oct 01 '21 at 14:26

2 Answers2

3

EDIT

If you paste the greek letters with unicode, it should work.
Example, for alpha (upper case) and tau (lower case), it is

"\u03B1"
"\u03C4"

You can change your variable's factors upstream so the legend changes itself, example:

#Simulating dataset
set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5)))
)

#Changing variable name into greek letters
df$sex <- ifelse(df$sex=="F", "\u03B1", "\u03C4")

#Histogram with greek letters legend
ggplot(df, aes(x=weight, fill=sex, color=sex)) +
geom_histogram(position="identity")

I think it var1 in your code should be changed upstream.
If that is not resolving the problem, maybe could you post a picture of your output vs what you would like to output.

For upper and lower case unicode, you can go on the following website
https://unicode-table.com/fr/sets/greek-symbols/

Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20
  • Thanks. But how can I put subscript? – Brian Smith Oct 01 '21 at 13:40
  • 1
    @BrianSmith Please do not use existing questions to ask other questions. Also, there exist multiple answers on SO and elsewhere on how to add sub/superscript to ggplot2 legends (e.g. [here](https://stackoverflow.com/questions/6202667/how-to-use-subscripts-in-ggplot2-legends-r). At least some previous research could be expected ;). Finally it is considered good practice to accept a correct answer so others might find it! – dario Oct 01 '21 at 14:00
  • @dario Thanks for your link. But still it did not solve my original problem of replacing letters with greek letters in `legend` – Brian Smith Oct 01 '21 at 14:05
  • 2
    Change `scale_x_discrete()` to `scale_fill_discrete()` and use @Yacine Hajji suggestion for unicode, i.e. `scale_fill_discrete(labels = c('x1' = "\u03B1", 'x2'= "\u03B2"))` – Skaqqs Oct 01 '21 at 14:31
  • @Skaqqs Nothing worked to my satisfaction, however your suggestion to use `scale_fill_discrete ` worked perfectly. Thanks for pointing this out. – Brian Smith Oct 01 '21 at 15:42
  • 1
    You're welcome. Please consider that everyone here kindly and correctly answered your question of how to add greek letters to ggplot. It would be great if you could please update your question to explain why you were having trouble with this and what the solution was. For example, you were using the wrong scale (x) for the aesthetic you wanted to specify (fill), and that using unicode strings OR `expression()` works perfectly fine for specifying symbols in plot text annotations. Good luck – Skaqqs Oct 01 '21 at 15:48
3

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`.

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

user2554330
  • 37,248
  • 4
  • 43
  • 90