3

I use the stat_poly_eq function from the ggpmisc package to add the regression equation in my ggplots. It works perfectly. However, I'd like to change the decimal mark of the equation from period to comma, and I can't figure out how to do this. Any suggestions?

data(mpg)

ggplot(mpg, aes(y = displ, x = hwy)) +
  geom_point(alpha = 0.5, color = "cadetblue") +
  geom_smooth(method = "lm", se = F, color = "black", size = 0.5) +
  stat_poly_eq(aes(label = paste(after_stat(eq.label),
                                 after_stat(rr.label),
                                 sep = "*plain(\",\")~~")),
               coef.digits = 3)
4
  • Try setting options(OutDec = ",") before running ggplot.
    – January
    Dec 15, 2022 at 14:31
  • It works for the equation, but not for the R-squared... I also would like an alternative that would not change my global options... Dec 15, 2022 at 14:34
  • Well, you can always oldopts = options(...) ; ggplot(...) ... ; options(oldopts). But it is weird that it does not work for the .
    – January
    Dec 15, 2022 at 14:45
  • Just for completeness. This was a bug in recent versions of 'ggpmisc'. It is now fixed in 'ggpmisc' 0.5.2 now in CRAN. Dec 19, 2022 at 23:09

3 Answers 3

2
data(mpg)
library(ggpmisc)

my_formula <- y ~ x
myformat <- "y = %s %s x, R²: %s"

ggplot(mpg, aes(y = displ, x = hwy)) +
  geom_point(alpha = 0.5, color = "cadetblue") +
  geom_smooth(method = "lm", se = F, color = "black", size = 0.5) +
  stat_poly_eq(
    formula = my_formula, output.type = "numeric",
    mapping = aes(
      label = 
        sprintf(
          myformat,
          format(stat(coef.ls)[[1]][[1, "Estimate"]], digits = 3, decimal.mark = ","),
          format(stat(coef.ls)[[1]][[2, "Estimate"]], digits = 2, decimal.mark = ","),
          format(stat(r.squared), digits = 2, decimal.mark = ",")))
  ) 

I have elaborated my answer based on this post.

I hope this helps you! 😀

3
  • You're welcome, I'm glad to hear it!
    – Esther
    Dec 15, 2022 at 15:33
  • This is a bug. Next version of 'ggpmisc' will obey the "OutDec" R option. I should have thought of this! (I am a native Spanish speaker...) Dec 15, 2022 at 23:43
  • It seems that there is not clean way to fix this: stackoverflow.com/questions/23586741/… I will most likely have to add to the package the gsub() kludge. The root of the problem is that 'ggpmisc' code makes use of sprintf(), while sprintf() uses the operating system's locale setting. Dec 16, 2022 at 14:42
2

'ggpmsic' 0.5.2 is now in CRAN. Bug fixed (I hope) Important: because of the way 'ggplot2' works the option must be set to the desired decimal marker when the plots is rendered (printer or exported to a graphical format). (The option as set when the gg object is created or saved to a variable does not affect the object itself.)

library(ggpmisc) # version >= 0.5.2
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

old.option <- options(OutDec = ",")

ggplot(mpg, aes(y = displ, x = hwy)) +
  geom_point(alpha = 0.5, color = "cadetblue") +
  geom_smooth(method = "lm", se = F, color = "black", linewidth = 0.5) +
  stat_poly_eq(aes(label = paste(after_stat(eq.label),
                                 after_stat(rr.label),
                                 sep = "*plain(\";\")~~")),
               coef.digits = 3)
#> `geom_smooth()` using formula = 'y ~ x'

options(old.option)

The plot using the new vesrion of 'ggpmisc'

I also replaced size with linewidth to avoid a warning from 'ggplot2' 3.4.0, and changed the comma in sep to a semicolon as it seems better when the decimal mark is a comma.

Created on 2022-12-20 with reprex v2.0.2

1

You can simply swap the periods for commas using gsub in the rr.label. The eq.label is parsed as an expression, so the period needs to be replaced with something like *paste(',')* for this to work:

ggplot(mpg, aes(y = displ, x = hwy)) +
  geom_point(alpha = 0.5, color = "cadetblue") +
  geom_smooth(method = "lm", se = F, color = "black", size = 0.5) +
  stat_poly_eq(aes(label = paste(gsub('\\.', "*paste(',')*", 
                                      after_stat(eq.label)),
                                 gsub('\\.', ",", after_stat(rr.label)),
                                 sep = "*plain(\",\")~~")),
               coef.digits = 3, size = 6)

enter image description here

1
  • Great! I had tried gsub but without success. Your suggestion works perfectly! Thank you! Dec 15, 2022 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.