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

`stat_poly_eq()` not showing equations in some cases #60

Closed wbvguo closed 3 months ago

wbvguo commented 3 months ago

Hi,

thanks for maintaining this package, I found the stat_poly_eq() might not show equations in some cases with warning message:

Warning: Upper limit outside search range. Set to the maximum of the parameter range.
--
Warning: Computation failed in `stat_poly_eq()`

Here is the code example with test data

library(tidyverse)
library(ggpmisc)

test_df = readRDS(ggpmisc_stat_poly_eq_df.rds)

test_df %>% 
  ggplot(aes(x = ref1, y = ref2, colour = grp)) +
  geom_point() +
  geom_smooth(method="lm" , color="black", fill='grey', se=TRUE, alpha = 0.5, linewidth =0.5) +
  stat_poly_eq(use_label("eq"), label.x.npc = 0.055, label.y.npc = 0.88, color = "black", coef.keep.zeros = FALSE) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed", color = "blue", alpha =0.5) +
  theme_bw() +
  facet_wrap(~grp)

image

Here the panel E's equation is missing. Do you have any idea why this happen, and perhaps any suggestions to make it work?

Thanks!

aphalo commented 3 months ago

@wbvguo Hi Wenbin, the error comes from the computation of the $R^2$ confidence interval. One can disable this computation by adding rsquared.conf.level = NA as shown below. This is anyway a bug that I need to fix, as this error should be handled more gracefully. Please, leave this issue open. Thanks for the report!

test_df %>% 
  ggplot(aes(x = ref1, y = ref2, colour = grp)) +
  geom_point() +
  geom_smooth(method="lm" , color="black", fill='grey', se=TRUE, alpha = 0.5, linewidth =0.5) +
  stat_poly_eq(use_label("eq"), label.x.npc = 0.055, label.y.npc = 0.88, color = "black", 
               coef.keep.zeros = FALSE, rsquared.conf.level = NA) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed", color = "blue", alpha =0.5) +
  theme_bw() +
  facet_wrap(~grp)
wbvguo commented 3 months ago

Thanks @aphalo, this is helpful!

aphalo commented 3 months ago

Fixed now, using a call to try().