kassambara / ggpubr

'ggplot2' Based Publication Ready Plots
https://rpkgs.datanovia.com/ggpubr/
1.1k stars 161 forks source link

Changing the number of significant digits shown with stat_regline_equation() #312

Open JoannaPeng opened 4 years ago

JoannaPeng commented 4 years ago

How to increase the number of significant digits from 2 (default) to 3 in stat_regline_equation()? So instead of 'y=-3.3 + x', it would show 'y = -3.35 + 1.02 x', where the untruncated intercept is -3.3462.

stat_regline_equation example

isaactpetersen commented 3 years ago

I have the same request. My slope value (0.96) doesn't show when using stat_regline_equation() in bookdown because it's getting rounded to 1.

japhir commented 3 years ago

also had this issue, it's better to use the package that inspired this function:

library(ggpmisc)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpmisc'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
df <- data.frame(x = c(1:100))
df$y <- 20 * c(0, 1) + 3 * df$x + rnorm(100, sd = 40)
df$group <- factor(rep(c("A", "B"), 50))
my.formula <- y ~ x

p <- ggplot(data = df, aes(x = x, y = y)) +
  geom_smooth(method = "lm", se=FALSE, formula = my.formula) +
  stat_poly_eq(formula = my.formula,
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               parse = TRUE, coef.digits = 5, f.digits = 5, p.digits = 10) +
  geom_point() +
  facet_wrap(~group)
p

Created on 2021-03-23 by the reprex package (v1.0.0)

https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph

JoannaPeng commented 2 years ago

I found this post is very useful and provides a good solution: https://stackoverflow.com/questions/66177005/im-using-stat-regline-equation-with-ggscatter-is-there-a-way-to-specify-the-si.

I actually put the code trace(ggpubr:::.stat_lm, edit = TRUE) directly in the R script, not in the console, right before the plotting code. This line will prompt a pop-up window where you can modify the significant digits of the coefficients to 3. Then hit 'save'. And then you can run the plotting code.

zx8754 commented 1 year ago

Another relevant SO post: