ggplot中的希腊字母注释

时间:2014-07-28 00:51:14

标签: r ggplot2

我想在ggplot中将希腊字母添加到文本中。这就是我想要做的事情:

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")

而不是&#34; theta&#34;,我想使用希腊字母theta。我试过这个link但不能这样做。我尝试了以下代码但没有发生任何事情:

temp <- list(bquote(theta == .(xIntercept)))
temp <- bquote(theta == .(xIntercept))
temp <- expression(theta, "=", xIntercept)
temp <- c(expression(theta), paste0("=", xIntercept))

1 个答案:

答案 0 :(得分:7)

您的链接指出annotate您应该使用parse = TRUE。所以

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

工作并给出一个希腊theta符号。 编辑:但是=符号也被解析,因此MrFlick指出你应该去

temp <- paste("theta == ", xIntercept)