ggplot 的 annotate() 中的星号

inv*_*tus 5 r ggplot2

可以使用 将文本添加到ggplot对象annotate()。但是,我无法弄清楚如何打印星号,因为据我所知,星号用于解析。

这是一个例子。假设我运行了一个非参数检验,我想打印检验统计量,其显着性水平以星为单位。不幸的是,这不起作用:

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + 
  geom_point() + 
  annotate('text', x = 7, y = 4, label="chi^2 == 2.50***", parse=TRUE)

Error in parse(text = as.character(lab)) : <text>:1:16: unexpected '*'
1: chi^2 == 2.50***
Run Code Online (Sandbox Code Playgroud)

在绘图对象之外定义标签 - 例如lab <- paste0("chi^2 == 2.5","***", sep=""),然后调用annotate('text', x = 7, y = 4, label=lab, parse=TRUE)- 也不起作用。

星号可以和 一起使用annotate()吗?

编辑对不起,我忽略了我想要编译希腊字母。理想情况下,文本将显示为 $$chi^2 = 2.50***$$

avi*_*seR 6

使用双引号和单引号的组合可以解决问题:

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + 
  geom_point() + 
  annotate('text', x = 7, y = 4, label='chi^2 == "2.50***"', parse=TRUE)
Run Code Online (Sandbox Code Playgroud)

您可以对 使用单引号label =,对不想annotate解析的字符串使用双引号。双引号外但单引号内的任何内容都将被解析。

在此处输入图片说明