Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greek letters in ggplot annotate

Tags:

r

ggplot2

I want to add greek letters into text in ggplot. Here is what I want to do:

library(ggplot2)
df <- data.frame(x =  rnorm(10), y = rnorm(10))
xIntercept <- mean(df$x)
yIntercept <- mean(df$y)
temp <- paste("theta = ", xIntercept) # This Works
ggplot(df, aes(x = x, y = y)) + geom_point() +
  annotate("text", x = xIntercept, y = yIntercept, label = temp, color = "blue")

Instead of "theta", I want to use Greek letter theta. I tried this link but cannot do it. I tried following codes but nothing happened:

temp <- list(bquote(theta == .(xIntercept)))
temp <- bquote(theta == .(xIntercept))
temp <- expression(theta, "=", xIntercept)
temp <- c(expression(theta), paste0("=", xIntercept))
like image 289
HBat Avatar asked Jul 28 '14 00:07

HBat


1 Answers

Your link states that in annotate you should use parse = TRUE. So

ggplot(df, aes(x = x, y = y)) + geom_point() +
  annotate("text", x = xIntercept, y = yIntercept, label = temp, color = "blue", parse = TRUE)

works and gives a greek theta symbol. EDIT: However the = sign is parsed too, so as MrFlick noted you should go for

temp <- paste("theta == ", xIntercept)
like image 87
Brouwer Avatar answered Oct 21 '22 03:10

Brouwer