rggplot2equationlmggpmisc

Show sqrt(y) ~ assign(sqrt(x)) relationship with `stat_poly_eq`, `stat_fit_tb` or `stat_fit_tidy`


require(utils)
x = runif(n = 10, min = 0.2, max = 1)
y = rnorm(n = 10, mean = 2.5, sd = .5)
df = expand.grid(x = x,
                y = y)

I used the code below to plot the sqrt(y) ~ assign(sqrt(x)) relationship and show the equation on the graph. The regression line shows, but the equation doesn't.

ggplot(df , aes(x = x, y = y)) +
  geom_point() +
  geom_smooth( method = "lm", se = TRUE, formula = sqrt(y) ~ asin(sqrt(x)), col = "black") +
stat_poly_eq(mapping = aes(label = after_stat(eq.label)), 
               formula =  sqrt(y) ~ asin(sqrt(x)))

Warning: [38;5;254mComputation failed in `stat_poly_eq()`[39m

enter image description hereCode sample was from https://cran.r-project.org/web/packages/ggpmisc/vignettes/model-based-annotations.html#stat_poly_eq-and-stat_poly_line

stat_fit_tb or stat_fit_tidy returned the same warning and no equation.

Thanks for stopping by.


Solution

  • If one uses a transformation as part of the model equation in a plot layer, the fitted function will not be plotted correctly, as the observations will not the transformed. The transformation should be applied to the scale, specially in the case of y. x and y in the formula are values of the aesthetics. This applies both to the fitted line and the equation.

    The computaion fails initially, because the orientation cannot deduced from the model formula. This can be solved by passing an argument to orientation = "x". However, after solving this problem a new error is triggered in the computation of the confidence interval for R2. This second error is a bug in the 'ggpmisc' package. Failure of the confidence interval computation should be handled better, by returning NAs for the confidence limits instead of failing to return all output.

    After I fix the bug, the code below should work. (I not only fixed the error in the code, but in addition simplified it as much as possible.)

    ggplot(df , aes(x = asin(sqrt(x)), y = sqrt(y))) +
      geom_point() +
      stat_poly_line() +
      stat_poly_eq(use_label("eq"), 
                   rsquared.conf.level = NA)
    

    A quick edit to the 'ggpmisc' code makes it possible to manually disable the confidence interval computation. This example is pathological in that R^2 is almost exactly zero. This bug-fix will be included in future version 0.5.4, meanwhile the fixed package can be installed from the git repository at GitHub.