6

I've found using stat_regline_equation (with ggscatter) to be really useful for quickly adding regression equations to plots, especially when I having multiple regressions on multiple facets. However, it seems to be stuck on 2 significant figures for terms. I know it is possible to extract coefficients from an lm and then display them with annotate. But with multiple facets that seems really daunting and finicky. Is there possibly a feature in the works (or already here, that I don't know about) to do this with stat_regline_eq? I have hope because you can specify digits with stat_cor for things like r-square and p-values! Any advice or help would be appreciated. Reproducible example below:

library(tidyverse)
library(ggpubr)

ggscatter(diamonds, x="carat", y="table", add="reg.line") +
  facet_wrap(~color) +
  stat_regline_equation(label.y=90)

summary(lm(table ~ carat, data=filter(diamonds, color=="D")))

output of ggscatter above

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 56.46453    0.05438  1038.3   <2e-16 ***
carat        1.42911    0.07255    19.7   <2e-16 ***

As you can see, the intercept shown in ggscatter for D is off by 0.5.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Carl
  • 83
  • 1
  • 7

1 Answers1

8

The underlying function that produced the equations is .stat_lm, an unexported function from the ggpubr package. As you've noted, the terms it produces all contain 2 significant figures. This is hard-coded into the function itself (you can see for yourself by running debugonce(ggpubr:::.stat_lm) before printing your plot, and step through the function yourself).

If you are using RStudio, you can enter trace(ggpubr:::.stat_lm, edit = TRUE) into the console and modify the function's code in the pop-up window. I haven't used other GUIs for R, but I assume the procedure should be similar. The lines you want to change are lines 13-14.

Demonstration with the diamonds dataset.

diamonds %>%
  filter(color == "D") %>%
  ggscatter(x = "carat", y = "table", add = "reg.line") +
  stat_regline_equation(label.y = 90)

Original lines 13-14 in the function:

  eq.char <- as.character(signif(polynom::as.polynomial(coefs), 
    2))

Alternative (if you want to specify by decimal place instead):

  eq.char <- as.character(round(polynom::as.polynomial(coefs), 
    1))

result1

Another alternative (if you want 5 significant figures instead of 2):

  eq.char <- as.character(signif(polynom::as.polynomial(coefs), 
    5))

result2

When the modification is no longer required, run untrace(ggpubr:::.stat_lm).

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    It's amazing that there is a tool that allows you to make temporary adjustments to packages you have loaded. I'm sure I will use `trace(... edit=TRUE)` at some point in the future. – Carl Feb 15 '21 at 17:02