2

I am currently using the code for stat_poly_eq in ggplot2 and was wondering if there is a way to always get the values on the graph in xe+1? So far it only gives me the Xx10^1 when the value is very high or very low. I would like it to always be in scientific notation for the stat(eq.label).

My code:

ggplot()+
  stat_poly_eq(aes(label =  paste(stat(eq.label), "*\", \"*", 
                                  stat(rr.label), "*\", \"*",
                                  stat(p.value.label), "*\"\"",
                                  sep = "")),
               formula = my.formula, parse = TRUE, size = 4)+

Thank you!

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
ZuZu
  • 23
  • 5

1 Answers1

2

Maybe something like this:

df <- data.frame(x = c(1:100))
df$y <- 20 + 30 * df$x + rnorm(100, sd = 80)

library(ggpmisc)

my_formula <- y ~ x

myformat <- "y = %s + %s x --- R²: %s"
ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_smooth(method = "lm", formula = my_formula, se = FALSE) +
  stat_poly_eq(
    formula = my_formula, output.type = "numeric",
    mapping = aes(
      label = 
        sprintf(
          myformat,
          format(stat(coef.ls)[[1]][[1, "Estimate"]], scientific = TRUE, digits =4),
          format(stat(coef.ls)[[1]][[2, "Estimate"]], scientific = TRUE, digits =4),
          formatC(stat(r.squared)))),
    vstep = 0.1
  ) 

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225