可以使用ggpubr包中的stat_regline_equation函数来在拟合直线上添加方程和R方和p值。

步骤如下:

  1. 安装ggpubr包:install.packages("ggpubr")

  2. 载入ggpubr包:library(ggpubr)

  3. 使用ggplot绘制散点图并添加拟合直线,例如:

ggplot(data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm")
  1. 在拟合直线上添加方程、R方和p值:
ggplot(data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm") +
  stat_regline_equation(
    aes(label = paste(..eq.label.., ..adj.rr.label.., sep = "~~~")),
    label.x.npc = "right",
    label.y.npc = 0.15,
    formula = y ~ x,
    parse = TRUE
  )

其中,stat_regline_equation函数中的参数解释如下:

  • aes(label = paste(..eq.label.., ..adj.rr.label.., sep = "~~~")):添加方程、R方和p值的标签,sep参数指定分隔符。

  • label.x.npc = "right":标签水平位置在拟合直线右侧。

  • label.y.npc = 0.15:标签垂直位置离拟合直线0.15个单位长度。

  • formula = y ~ x,指定拟合方程为y = a + bx。

  • parse = TRUE:允许使用LaTeX语法。

参考文献:

https://rpkgs.datanovia.com/ggpubr/reference/stat_regline_equation.html

r语言ggplot绘图之后如何在拟合直线上添加方程和r方和p值方简单方法

原文地址: https://www.cveoy.top/t/topic/bf8f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录