Reputation: 4200
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.
Upvotes: 3
Views: 3293
Reputation: 2698
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\\]")))
Upvotes: 4
Reputation: 13803
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.
Upvotes: 0