aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

Error with `ggpmisc::stat_poly_eq()`: `! out[1] <= out[2] is not TRUE` #65

Closed emmansh closed 3 months ago

emmansh commented 3 months ago

Already posted in Stack Overflow (here), but I believe this is a bug so opening it here.


I get the following error with stat_poly_eq():

Caused by error in check_output():

! out[1] <= out[2] is not TRUE

Example Data

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

df <- 
  data.frame(foo = c(-16, -15, -11, -10, -7, -6, -4, -3),
             bar = c(NA, 12, 18, -34, 37, -18, -26, 36))  

length(df$foo)
#> [1] 8
length(df$bar)
#> [1] 8

df |> 
  ggplot(aes(x = foo, y = bar)) +
  stat_poly_line() +
  stat_poly_eq(use_label(c("eq", "R2")))
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_line()`).
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_eq()`).
#> Warning in ci_f_ncp(stat, df1 = df1, df2 = df2, probs = probs): Upper limit
#> outside search range. Set to the maximum of the parameter range.
#> Warning: Computation failed in `stat_poly_eq()`.
#> Caused by error in `check_output()`:
#> ! out[1] <= out[2] is not TRUE

Created on 2024-05-26 with reprex v2.0.2


Searching around I found this answer from Stack Overflow. Indeed, using ggpubr gives the expected output:

library(ggplot2)
library(ggpubr)

df <- 
  data.frame(foo = c(-16, -15, -11, -10, -7, -6, -4, -3),
             bar = c(NA, 12, 18, -34, 37, -18, -26, 36))  

df |> 
  ggplot(aes(x = foo, y = bar)) +
  geom_smooth(method = lm) +
  stat_cor(aes(label = paste(after_stat(rr.label))), # adds R^2 value
           r.accuracy = 0.01, size = 4, vjust = 0) +
  stat_regline_equation(aes(label = after_stat(eq.label)))
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_smooth()`).
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_cor()`).
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_regline_equation()`).

Created on 2024-05-26 with reprex v2.0.2

aphalo commented 3 months ago

This error is caused by an attempt to compute a confidence interval with too few observations or NAs. Please, see #60. A fix is implemented in the main branch here. Can be installed from R-Universe.

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

df <- 
  data.frame(foo = c(-16, -15, -11, -10, -7, -6, -4, -3),
             bar = c(NA, 12, 18, -34, 37, -18, -26, 36))  

length(df$foo)
#> [1] 8
length(df$bar)
#> [1] 8

df |> 
  ggplot(aes(x = foo, y = bar)) +
  stat_poly_line() +
  stat_poly_eq(use_label(c("eq", "R2")))
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_line()`).
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_eq()`).
#> Warning in ci_f_ncp(stat, df1 = df1, df2 = df2, probs = probs): Upper limit
#> outside search range. Set to the maximum of the parameter range.
#> Warning in compute_group(...): CI computation error: Error in check_output(cint, probs = probs, parameter_range = c(0, 1)): out[1] <= out[2] is not TRUE

Created on 2024-05-27 with reprex v2.1.0

or with the CRAN version of 'ggpmisc' or to silence the error message, please add rsquared.conf.level = NA to the call.

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

df <- 
  data.frame(foo = c(-16, -15, -11, -10, -7, -6, -4, -3),
             bar = c(NA, 12, 18, -34, 37, -18, -26, 36))  

length(df$foo)
#> [1] 8
length(df$bar)
#> [1] 8

df |> 
  ggplot(aes(x = foo, y = bar)) +
  stat_poly_line() +
  stat_poly_eq(use_label(c("eq", "R2")), rsquared.conf.level = NA)
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_line()`).
#> Warning: Removed 1 row containing non-finite outside the scale range
#> (`stat_poly_eq()`).

Created on 2024-05-27 with reprex v2.1.0