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.