ggplot中的希腊字母注释

6

我想在 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))
1个回答

9
你的链接指出在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)

2
temp <- paste("theta == ", xIntercept) 会保留等号。 - MrFlick
工作得很好。谢谢。 - HBat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接