我想在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")
Run Code Online (Sandbox Code Playgroud)
而不是"theta",我想使用希腊字母theta.我试过这个链接但不能这样做.我尝试了以下代码但没有发生任何事
temp <- list(bquote(theta == .(xIntercept)))
temp <- bquote(theta == .(xIntercept))
temp <- expression(theta, "=", xIntercept)
temp <- c(expression(theta), paste0("=", xIntercept))
Run Code Online (Sandbox Code Playgroud)
您的链接指出annotate
您应该使用parse = TRUE
.所以
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = xIntercept, y = yIntercept, label = temp, color = "blue", parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
工作并给出一个希腊theta符号.编辑:然而=
标志也被解析,所以MrFlick指出你应该去
temp <- paste("theta == ", xIntercept)
Run Code Online (Sandbox Code Playgroud)