1

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Alex
  • 96
  • 6
  • 1
    What if you tried `output.type="text"`, would that be sufficient? `stat_cor(show.legend = F ,label.y = c(18), method = "pearson", output.type = "text")` – MrFlick Nov 14 '22 at 14:29
  • I just realized that this question is probably a duplicate, but the questioner wasn't aware that it was likely due to having set the output decimal as a comma: https://stackoverflow.com/questions/71457211/error-with-ggscatterplot-function-from-ggpubr-package – Alex Nov 14 '22 at 14:31
  • 1
    @MrFlick Thanks! This kind of works! I dont get the Error message and the R value decimal seperator is a comma, however strangely the decimal seperator in the "p < 2.2e-16" remains a point! – Alex Nov 14 '22 at 14:41
  • I think you are correct. The "answer" to that question does seem to miss the root of the problem. You have likely diagnosed the real issue correctly. Ideally the `ggpubr` would be wrapping these values in quotes before passing them in as expressions. – MrFlick Nov 14 '22 at 14:41
  • 1
    Yeah, that’s a bug in the ‘ggpubr’ package. Please consider it reporting to the maintainers. – Konrad Rudolph Nov 14 '22 at 14:45
  • 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. It would probably be easier just to create your own text annotation for the plot with the formatting you want. – MrFlick Nov 14 '22 at 14:48
  • @KonradRudolph I don't have github experience so feel free to report the error to them if it is simple/possible for you otherwise I'll try to do it. – Alex Nov 14 '22 at 14:56
  • The value "2.2e-16" is hard coded as a string in the `get_p_label` function I linked to. There's no setting you can change that will alter the contents of that string. – MrFlick Nov 14 '22 at 15:01
  • Comment about my second comment: The "p < 2.2e-16" is because it is hardcoded as a string as @MrFlick mentioned in this line: https://github.com/kassambara/ggpubr/blob/ac5a01f586a5f0b47260fed071e79be36ef3f8ea/R/stat_cor.R#L221 However, for all cases were p > 2.2e-16 it actually works as expected, so this is just a problem with the test data. – Alex Nov 14 '22 at 15:12
  • @MrFlick Feel free to post your first comment as an answer so that I can accept it, so that people can find the solution, it would have saved me half a days work :D – Alex Nov 14 '22 at 15:16
  • 1
    @KonradRudolph I have reported it as an issue: https://github.com/kassambara/ggpubr/issues/512 – Alex Nov 15 '22 at 08:01

1 Answers1

0

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.

Alex
  • 96
  • 6