3

I am trying to include mathematical signs into ggplot axis labels. Consider the following code.

library(ggplot2)
df <- data.frame(dose=c("D0.5", "D1", "D2"), len=c(4.2, 10, 29.5))
ggplot(data=df, aes(x=dose, y=len, group=1)) +
  ylab("length")+
  geom_line()+
  geom_point()

I would now like to include the range of len ("Length") in the y axis description with the notation Length ∈ [0, 10] but can't find a way to get the "element of" sign into the label.

Ivo
  • 3,890
  • 5
  • 22
  • 53

2 Answers2

3

There's a great post here that goes through the different ways, here I use expression

ggplot(data=df, aes(x=dose, y=len, group=1)) +
  ylab("length")+
  geom_line()+
  geom_point() +
  ylab(expression("Length " ~ epsilon ~ " [0, 10]"))

EDIT: Since the symbol for element of is \in, the expression code does not work since in is a built-in function. There are likely workaround, but I had to resort to using the latex2exp package

library(latex2exp)


ggplot(data=df, aes(x=dose, y=len, group=1)) +
  ylab("length")+
  geom_line()+
  geom_point() +
  ylab(TeX(sprintf("Length $\\in$ \\[0, 10\\]")))
astrofunkswag
  • 2,608
  • 12
  • 25
  • Interesting work around. Similarly, using `"\U03F5"` gives you an epsilon. Unfortunately, it's not quite the same as the "element of" unicode character OP was requesting. – chemdork123 May 04 '20 at 18:33
  • 1
    typo on symbol to use, thanks for pointing out. Latex symbol is `\in` which is built in and leads to issues, seems like the only symbol that won't work w this method. I think it's useful to share still – astrofunkswag May 04 '20 at 18:51
  • 1
    This, too only gives me the square - is this an issue with my system - and how could I address this? – Ivo May 05 '20 at 13:44
0

For special symbols, you can reference their Unicode value via the escape character \U####. ∈ is Unicode (U+2208), so using \U2208 can be used to paste the ∈ symbol.

So:

> 'Length \U2208 [0, 10]'
[1] "Length ∈ [0, 10]"

And then you can just use ylab('Length \U2208 [0, 10]') in your plot code.

chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Thank you. Is there a way to address the issue of the symbol not showing - but instead the square typical for encoding errors? – Ivo May 04 '20 at 17:10
  • 1
    Doesn't look like that character is supported in ggplot from what I can see. I've tried a few things, but have not found a good way to address - I'm able to get the output in the console, but not on a plot. – chemdork123 May 04 '20 at 18:31
  • yes, the same happens to me - the console works fine but not with `ggplot`. Is there something to be done? – Ivo May 05 '20 at 13:45
  • I've gone the route of trying to adjust the font (see `extrafonts` package), but no luck so far. It also does not seem to matter if I export via png (graphics device) or pdf (cairo_pdf or standard). It also does not print correctly using `annotate()` or `annotate_custom()`, which leads me to find that the issue is not related at all to how the functions create the plot, but how the plot is realized via the graphics device in R. The only workaround would be to create an image file with the character and use a custom annotation to place that outside the plot. – chemdork123 May 05 '20 at 14:43