ggplot散點(diǎn)圖加相關(guān)系數(shù)

計算一組數(shù)據(jù)有沒有相關(guān)性以及相關(guān)程度時,可以使用cor(),以及cor.test()計算顯著性,如下所示,我們想計算這兩種花的長度有沒有相關(guān)性。

head(iris)
plot(iris$Sepal.Length,iris$Petal.Length)
cor(iris$Sepal.Length,iris$Petal.Length)
#[1] 0.8717538
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

我們可以手動將計算的相關(guān)系數(shù)以及p-value加在圖上,也可以直接使用ggpubr包中的stat_cor()來將散點(diǎn)圖直接標(biāo)記相關(guān)系數(shù)以及p-value。

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")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容