前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot散点图加相关系数

ggplot散点图加相关系数

作者头像
生信编程日常
发布2020-12-29 10:34:54
5.5K0
发布2020-12-29 10:34:54
举报

计算一组数据有没有相关性以及相关程度时,可以使用cor(),以及cor.test()计算显著性,如下所示,我们想计算这两种花的长度有没有相关性。

代码语言:javascript
复制
head(iris)
plot(iris$Sepal.Length,iris$Petal.Length)
代码语言:javascript
复制
cor(iris$Sepal.Length,iris$Petal.Length)
#[1] 0.8717538
代码语言:javascript
复制
cor.test(iris$Sepal.Length,iris$Petal.Length)


#   Pearson's product-moment correlation

#data:  iris$Sepal.Length and iris$Petal.Length
#t = 21.646, df = 148, p-value < 2.2e-16
#alternative hypothesis: true correlation is not equal to 0
#95 percent confidence interval:
# 0.8270363 0.9055080
#sample estimates:
#     cor 
#0.8717538

我们可以手动将计算的相关系数以及p-value加在图上,也可以直接使用ggpubr包中的stat_cor()来将散点图直接标记相关系数以及p-value。

代码语言:javascript
复制
library(ggplot2)
library(ggpubr)
ggplot(iris, aes(x=iris$Sepal.Length, y=iris$Petal.Length)) + 
  geom_point()+ geom_smooth(method = 'lm', se = F, color = 'red')+theme_bw()+stat_cor(data=iris, method = "spearman")
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档