我正在使用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
Run Code Online (Sandbox Code Playgroud)
我会关闭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()
Run Code Online (Sandbox Code Playgroud)
我们使用eq.with.lhs=FALSE关闭自动加上的y=,然后我们paste()在hat(y)对向前方(与等号)。请注意,格式来自?plotmath帮助页面。