Home > Net >  Adding Greek letters in legend of GGPLOT
Adding Greek letters in legend of GGPLOT

Time:10-02

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?

CodePudding user response:

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/

CodePudding user response:

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)

  • Related