r语言 - 希腊字母在ggplot注释



我想在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")

我想用希腊字母theta代替"theta"。我试过这个链接,但做不到。我尝试了以下代码,但没有任何反应:

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

您的链接声明在annotate中您应该使用parse = TRUE。所以

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

可以工作,并给出希腊的θ符号。编辑:然而=符号也被解析了,所以正如MrFlick指出的那样,你应该选择

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

最新更新