1、gglmannotate包

帮助文档

#安装
devtools::install_github("wilkox/gglmannotate")
#使用 geom_lmannotate() 函数添加统计指标
library(ggplot2)
library(gglmannotate)

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_lmannotate()

Untitled

#分组颜色映射:
ggplot(mpg, aes(x = displ, y = hwy, colour = class)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_lmannotate()

Untitled

#分面
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_lmannotate() +
  facet_wrap(~ class)

Untitled

2、annotate()添加

library(ggplot2)
library(gglmannotate)

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm") +
  #geom_lmannotate()+
  annotate("text",x=5.5,y=43,label="y=-3.5x+36",size=4,color='black',hjust=0)+
  annotate("text",x=5.5,y=40,label=expression(paste(italic("P")<0.001,"   " ,r^2==0.59)),size=4,color='black',hjust=0)

Untitled

3、ggpubr中的stat_cor()函数

缺点:似乎只能简单线性拟合,不能写出n

优点:P是小写,可以得出R,R²,p,R

library(ggpubr)

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point()+
  geom_smooth(formula = y~x, method = "lm")+
  stat_cor(formula=y~x, method = "pearson", aes(label = paste(..rr.label.., ..r.label.., ..p.label.., sep = "*")), p.accuracy = 0.001, label.x.npc="right",hjust=1) 

Untitled

4、ggpmisc中的stat_poly_eq()函数

缺点:不能写出R,P是大写

优点:可以写出拟合方程,包括各种方程,可以写出n,可以写出AIC、BIC等