geom_text中的数学符号错误

Ale*_*der 2 math r ggplot2

在文本中插入数学符号应该不是那么复杂我想!

OTH,甚至用数学表达式查看类似的例子 ggplot2 facet_wrap

我仍然无法插入Ω(Omega)符号geom_text!

假设您有基本的散点图,并且您希望将(Omega)数学符号的平均值添加到每个方面,

mean.Petal <- aggregate(iris["Petal.Width"], iris["Species"], mean)
    Species     Petal.Width
1     setosa       0.246
2 versicolor       1.326
3  virginica       2.026

ggplot(iris) +
  geom_point(aes(y=Sepal.Length,x=Sepal.Width ,col=factor(Species))) + 
  facet_wrap(~ Species)+
  geom_text(data = mean.Petal, parse = TRUE,
            aes(x = 4.5, y = 7, label=sprintf('mean_Petal=%.2f %s', 
                                               round(Petal.Width,digits=2),'Omega')))
Run Code Online (Sandbox Code Playgroud)

解析时出错(text = as.character(lab)):: 1:17:意外符号1:mean_Petal = 0.25欧米茄

另一个尝试

geom_text(data = mean.Petal, parse = TRUE,
          aes(x = 4.5, y = 7, label=paste('mean_Petal=', 
                                  round(Petal.Width,digits=2),expression(Omega),sep=' ')))
Run Code Online (Sandbox Code Playgroud)

解析时出错(text = as.character(lab))::: 1:18:意外符号1:mean_Petal = 0.25欧米茄

Mar*_*ius 8

使用geom_textparse = TRUE,您希望将与plotmath表达式对应的字符串放在一起,这样您就可以:

ggplot(iris) +
    geom_point(aes(y=Sepal.Length,x=Sepal.Width ,col=factor(Species))) + 
    facet_wrap(~ Species)+
    geom_text(data = mean.Petal, parse = TRUE,
              aes(x = 3, y = 7, 
                  label=paste("'Mean petal' ==", round(Petal.Width, digits=2), "* Omega")))
Run Code Online (Sandbox Code Playgroud)