Search code examples
rggplot2ggpmisc

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


I don't understand why I get the following error with stat_poly_eq():

Caused by error in check_output():

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

I've seen this answer but below I show a much simpler scenario that produces the error.

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

You might suggest that the one NA value causes the error. So how come moving the NA elsewhere works fine and I get the equation?

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

df_2 |> 
  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()`).

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


Solution

  • This is an unhandled error from the computation of the confidence interval of R^2. It has been fixed some days ago in the main branch of the under development version of 'ggpmisc' in GitHub and available through the R-Universe at https://aphalo.r-universe.dev/ggpmisc#.

    Meanwhile, using version 0.5.6 from CRAN, the solution is to disable the computation of the CI by passing rsquared.conf.level = NA in the call, as shown below.

    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