设置完后输出小数到逗号使用 option 命令,当使用函数 stat_cor 将 Pearson 相关结果包含在 ggplot 中时,出现以下错误:
Error in parse(text = text[[i]]) : <text>:1:16: unexpected ','
1: italic(R)~`=`~0,
^
下面是一个最小的可重现示例:
library(ggplot2)
library(ggpubr)
options(OutDec= ",") #set decimal seperator to a comma
plot<-ggplot(economics,aes (x=pce/10000, y=uempmed)) +
geom_point(size = 2)+
plot #works fine and axis labels correctly have commas as decimal seperator
options(OutDec= ",") #set decimal seperator to a comma
plot<-ggplot(economics,aes (x=pce/10000, y=uempmed)) +
geom_point(size = 2)+
stat_cor(show.legend = F ,label.y = c(18), method = "pearson")
plot #ERROR, adding ,decimal.mark = "," does not work
#Leads to error:
#Error in parse(text = text[[i]]) : <text>:1:16: unexpected ','
#1: italic(R)~`=`~0,
# ^
options(OutDec= ".") #set decimal seperator to a dot and it works as expected
plot<-ggplot(economics,aes (x=pce/10000, y=uempmed)) +
geom_point(size = 2)+
stat_cor(show.legend = F ,label.y = c(18), method = "pearson")
plot #correct output
我搜索了帮助函数以查看哪些参数可以传递给 stat_cor ,它导致传递给 geom_text 或 geom_label 的其他参数。并在那里其他参数传递给layer()。所以我尝试包括,decimal.mark = ","
and
, labels = label_number(decimal.mark = "," )
在 stat_cor 函数中,但这不起作用。
这是我在谷歌搜索时发现的最接近的东西,但它对我没有帮助,因为它是相同的问题,但在不同的函数中(geom_sf)https://github.com/tidyverse/ggplot2/issues/3365不过,我认为问题的根源已经得到了解释:
**clauswilke 于 2019 年 6 月 17 日发表评论 **
据我所知,出现这个特殊问题是因为 ggplot 创建了一个绘图数学表达式,然后将其转换为字符串,然后再次解析该字符串。在该解析阶段,无法理解以逗号作为分隔符的数字.
我想可以通过使用 geom_annotate 或 geom_label 来解决,但这意味着我必须手动包含大量图表的所有皮尔逊相关性测试结果,所以我更喜欢直接修复。