【1.6】散点图加入关联分析曲线--ggscatter

函数介绍

stat_cor(mapping = NULL, data = NULL, method = "pearson",
  label.sep = ", ", label.x.npc = "left", label.y.npc = "top",
  label.x = NULL, label.y = NULL, geom = "text", position = "identity",
  na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)

具体例子:

# Load data
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)

# Scatter plot with correlation coefficient
#:::::::::::::::::::::::::::::::::::::::::::::::::
sp <- ggscatter(df, x = "wt", y = "mpg",
   add = "reg.line",  # Add regressin line
   add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
   conf.int = TRUE # Add confidence interval
   )
# Add correlation coefficient
sp + stat_cor(method = "pearson", label.x = 3, label.y = 30)

# Color by groups and facet
#::::::::::::::::::::::::::::::::::::::::::::::::::::
sp <- ggscatter(df, x = "wt", y = "mpg",
   color = "cyl", palette = "jco",
   add = "reg.line", conf.int = TRUE)
sp + stat_cor(aes(color = cyl), label.x = 3)

备注:

数据前期处理

library(reshape2)
aaa1 <- aa1[,c(5:6,7,8,9,10)]
aaaa1 <- melt(aaa1,id.vars='V5',value.name="value",variable.name='bq')

参考资料:

http://www.sthda.com/english/rpkgs/ggpubr/reference/stat_cor.html

个人公众号,比较懒,很少更新,可以在上面提问题:

更多精彩,请移步公众号阅读:

Sam avatar
About Sam
专注生物信息 专注转化医学