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))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With