Having set the output decimal to a comma with the option command I get the following error, when using the function stat_cor to include Pearson correlation results in a ggplot:
Error in parse(text = text[[i]]) : <text>:1:16: unexpected ','
1: italic(R)~`=`~0,
^
Below a minimal reproducible example:
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
I searched through the help functions to see which arguments can be passed to stat_cor and it leads to other arguments to pass to geom_text or geom_label. and there to Other arguments passed on to layer().
So I tried including
,decimal.mark = ","
and
, labels = label_number(decimal.mark = "," )
in the stat_cor function but that did not work.
This is the closest thing I have found while googling but it did not help me as it is the same problem but in a different function (geom_sf) https://github.com/tidyverse/ggplot2/issues/3365 However, I think there the root of the issue is explained:
**clauswilke commented on Jun 17, 2019 ** As far as I can tell, this particular problem arises because ggplot creates a plotmath expression, then converts it into a string, and then parses the string again. At that parsing stage, the number with a comma as separator is not understood.
I guess there could be a workaround by using geom_annotate or geom_label but that means I would have to manually include all of the pearson correlation test results for a lot of graphs so I would prefer a direct fix.
CodePudding user response:
This is a bug in the package ggpubr as it cannot handle commas as decimal seperators. For more information see here or in the comments:
MrFlick: Specifically I think it's in this line https://github.com/kassambara/ggpubr/blob/ac5a01f586a5f0b47260fed071e79be36ef3f8ea/R/stat_cor.R#L207 . Those values should be wrapped in quotes to prevent the parsing error. And the problem with the persistent decimal point in the p-values comes from https://github.com/kassambara/ggpubr/blob/ac5a01f586a5f0b47260fed071e79be36ef3f8ea/R/stat_cor.R#L219 . It doesn't look like the package really support a comma separator. –
However, there is a workaround (pointed out by @MrFlick) by passing the argument , output.type="text"
in the stat_cor function e.g.:
stat_cor(show.legend = F ,label.y = c(18), method = "pearson", output.type="text")
However, there is an edge case where this does not work, if p < 2.2e-16 (as with the example data) as the value of p is passed as a string and not a number. Otherwise it works though.