stat_poly_eq 中的 R 科学记数法

标签 r ggplot2 scientific-notation ggpmisc

我目前正在 ggplot2 中使用 stat_poly_eq 的代码,想知道是否有办法始终获取 xe+1 中图表上的值?到目前为止,当值非常高或非常低时,它只给我 Xx10^1。我希望 stat(eq.label) 始终采用科学记数法。

我的代码:

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)+

谢谢!

最佳答案

也许是这样的:

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

关于stat_poly_eq 中的 R 科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70315747/

相关文章:

r - 使用facet_wrap时显示每个散点图的回归方程和R^2

java - 什么是科学记数法以及为什么在 Java 中科学记数法要重复打印?

gnuplot:读取科学格式数字(非标准形式)的静默错误

R plot撤消线段?

r - 如何在 pROC 中绘制具有置信区间的多条 roc 曲线?

r - 从由特殊字符包围的字符串中提取年份数字

ios - UIStepper.value 变成科学计数法

将单元格中包含公式的Excel文件读取到R中

r - 多页,每页有几个 ggplot2 图表和表格,来自多个数据框,每个页面都是一个共同因素的水平

r - 如何更改 ggplot2 中复杂填充的颜色?