R 包 ggpmisc : Putting hat on y in Regression Equation

标签 r ggplot2 ggpmisc

我正在使用 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

enter image description here

最佳答案

我会关闭 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帮助页面。

enter image description here

关于R 包 ggpmisc : Putting hat on y in Regression Equation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35585813/

相关文章:

r - 如何在 ggarrange 中编辑常见的图例标题?

R:根据其他因素在ggplot中拆分数据

r - 用组名及其方程标记 ggplot,可能用 ggpmisc?

r - 用 ggpmisc 显示 nls 模型的方程

R:向ggplot2添加水平线

java - 从 Java 执行时,R 脚本无法读取 .Rda 文件

r - 将因子级别从 df 的字符串版本应用到 df 的数字版本

r - 组合由 R base、lattice 和 ggplot2 创建的图

r - 从字符串创建公式调用