我正在使用 R
包裹ggpmisc
.想知道如何在回归方程中将帽子放在 y 上,或者如何在图形的回归方程中获得自定义响应和解释变量名称。
library(ggplot2)
library(ggpmisc)
df <- data.frame(x1 = c(1:100))
set.seed(12345)
df$y1 <- 2 + 3 * df$x1 + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x1, y = y1)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
stat_poly_eq(formula = y ~ x,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
最佳答案
我会关闭 y
的默认值粘贴并构建您自己的公式。例如
ggplot(data = df, aes(x = x1, y = y1)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE,
aes(label = paste("hat(italic(y))","~`=`~",..eq.label..,"~~~", ..rr.label.., sep = "")),
parse = TRUE) +
geom_point()
我们使用
eq.with.lhs=FALSE
关闭 y=
的自动包含然后我们paste()
hat(y)
在前面(带有等号)。请注意,格式来自 ?plotmath
帮助页面。关于R 包 ggpmisc : Putting hat on y in Regression Equation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35585813/