ggplot用希腊符号注释和(1)撇号或(2)在文本之间

tre*_*rop 4 r annotate ggplot2

无法在ggplot注释中添加希腊字母,当其中之一:它夹在其他文本之间,或者所讨论的文本包含撇号.

例如,以下工作正常:

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp<-paste("rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
    annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,ggplot会爆炸:

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp<-paste("Spearman's rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
    annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)
Run Code Online (Sandbox Code Playgroud)

ggplot对这些特殊字符非常敏感.其他帖子似乎没有解决这个问题(如果没有,请道歉).提前致谢.

Kei*_*itt 7

如果有人尝试在事先未知相关变量的动态场景中采用此方法,您还可以使用以下方法将表达式的转义版本创建为字符串:

df <- data.frame(x =  rnorm(10), y = rnorm(10))

spearmans_rho <- cor(df$x, df$y, method='spearman')
plot_label <- sprintf("\"Spearman's\" ~ rho == %0.2f", spearmans_rho)

ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  annotate("text", x = mean(df$x), y = mean(df$y), parse = T, label=plot_label)
Run Code Online (Sandbox Code Playgroud)


Sco*_*ott 5

我感到沮丧.除了使用expression@baptiste评论的标签之外,诀窍是将标签传递as.characterggplot.

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp <- expression("Spearman's"~rho == 0.34)
ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  annotate("text", x = mean(df$x), y = mean(df$y), 
           parse = T, label = as.character(temp))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述