0

I would like to include greek symbols in my ggplot2 legend as follows:

1

However, the greek symbols appear in the legend as:

2

The relevant part of the code generating this graph:

  data <- read_excel("file.xlsx", sheet=sheet)
  data <- melt(data, id.vars = c("X"))
  
  g <- ggplot(data, aes(x=X))
  g <- g + geom_ma(aes(y=value, color=variable, linetype=variable))

It is clear that R does not by default support unicode-based greek symbols inserted into Excel files, which are then melted using the reshape2 library. How does one achieve the correct behaviour?

Other resources on the internet suggest the usage of expression() or bquote(), but these approaches won't work with the method I use for generating graphs (with reshape2).

user15116257
  • 93
  • 1
  • 5
  • 1
    I don't have your data but it is certainly possible to plot greek symbols in ggplot2. Search on this forum and you will find some approaches. I can't tell which step you're running is the source of the issue, whether it's the loading step, the reshaping, or the plotting. – Jon Spring May 08 '23 at 17:32
  • I like the `ggtext` package for specifying typographical features like html. For instance, `library(ggplot2); library(ggtext); df <- data.frame(x = 5, y = 20:25, label = c("α", "β", "γ", "δ", "ε", "ζ")); ggplot(df, aes(x, y, label = label)) + geom_richtext()` – Jon Spring May 08 '23 at 17:33
  • Does this answer your question? [How to use Greek symbols in ggplot2?](https://stackoverflow.com/questions/5293715/how-to-use-greek-symbols-in-ggplot2) – Michael Roswell May 09 '23 at 14:19

1 Answers1

0

I think you can use bquote() to add greek symbols to the legend. I understand your code such that variable contains the Greek symbols, or that it at least should contain them.

df <- data.frame(A = 5, B = 20:21, variable = LETTERS[1:2])
ggplot(data = df, aes(x = A, y = B, color = variable)) +
  geom_point() +
  scale_color_discrete(labels = c(bquote(~ alpha), bquote(~ beta)))
  • Although the OP's problem isn't very clearly stated yet, they do mention that solutions using `bquote` are not what they're looking for. – Michael Roswell May 09 '23 at 14:21
  • I am not sure. The original post says the goal is to have Greek letters in the legend, which is what the proposed code does. If the goal is to import Greek letters correctly into R, bquote() of course does not help. It is then also not a plotting issue, but a data import issue. – Ingo Rohlfing May 10 '23 at 17:31