我目前正在 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
)
关于stat_poly_eq 中的 R 科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70315747/