【问题标题】:Math symbol error in geom_textgeom_text 中的数学符号错误
【发布时间】:2017-07-02 23:04:30
【问题描述】:

在文本中插入数学符号应该没有我想象的那么复杂!

OTH,即使是类似的例子 ggplot2 facet_wrap with mathematical expression

我仍然无法在geom_text 中插入Ω(欧米茄)符号!

假设您有基本的散点图,并且您想用(欧米茄)数学符号向每个方面添加平均值,

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')))

解析错误(text = as.character(lab)) : 1:17: 意外 符号1:mean_Petal=0.25 Omega

再试一次

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=' ')))

解析错误(text = as.character(lab)) : 1:18: 意外 符号1:mean_Petal= 0.25 Omega

【问题讨论】:

  • 下面给出欧米茄符号。 geom_text(data = mean.Petal, parse = TRUE, aes(x = 2.5, y = 7, label=paste('mean_Petal=', expression(Omega), sep=' ')))
  • round(Petal.Width, digits=2) 的目标是什么?
  • @DiscoSuperfly 谢谢。 round(Petal.Width, digits=2) 我需要“欧米茄”之前的平均值。所以我需要它们!

标签: r math ggplot2


【解决方案1】:

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")))

【讨论】:

  • 谢谢。这就是我要找的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多