stat_poly_eq() removes significant trailing zeros in displayed coefficients

Create issue
Issue #22 on hold
Chun Lin created an issue

This post was copied over from the original post on Github, with a slightly modified title.

The problem

Settings in coef.digits and rr.digits are overridden to trim trailing zeros in the formatted coefficients and R-square, even if the zeros are part of the significant figures.

A reproducible example (R 3.5.2 and ggpmisc 0.3.0)

If coef.digits = 2, a slope of 1.04 should be shown as 1.0. Instead, 1.0 was simplified as 1 and subsequently not displayed. The resultant equation is misleading as the regression line is clearly unparallel with the 1:1 line as shown in the example code below:

library(tidyverse)
library(ggpmisc)

set.seed(2017)

tdf <- data.frame(
    x = 1:100,
    y = 1.04 * 1:100 + rnorm(100)
)

ggplot(aes(x = x, y = y), data = tdf) + 
    geom_point() + 
    geom_smooth(method = "lm") +
    stat_poly_eq(
        aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
        formula = y ~ x,
        coef.digits = 2,
        rr.digits = 3,
        parse = TRUE
    ) +
    geom_abline(slope = 1, intercept = 0, linetype = 2)

Comments (6)

  1. Pedro J. Aphalo repo owner

    Revised code to generate the correct "expression text" for parsing, but trailing zeros are still being dropped later.

  2. Pedro J. Aphalo repo owner

    The default of `coef.digits = 3', avoids the problem in the example above without solving it in general. Using 2 significant digits is too little for coefficient estimates. Now a warning is issued when fewer digits are used.

  3. Pedro J. Aphalo repo owner

    The default of `coef.digits = 3', avoids the problem in the example above without solving it in general. Using 2 significant digits is too little for coefficient estimates. Now a warning is issued when fewer digits are used. This is a temporary fix, but should help in most cases.

  4. Log in to comment