簡(jiǎn)要介紹相關(guān)性及PCA分析
1、 相關(guān)性分析
本次使用數(shù)據(jù)如下所示:

- 加載數(shù)據(jù)
data <- read.table('quality.txt',header = T,sep = "\t",row.names = 'Samples')
- 相關(guān)性分析
library(corrplot)
corrplot::corrplot(cor(data),type = 'upper',
addCoef.col = 'black')
## 參數(shù):
type: 上半部分
a ddCoef.col: 添加相似值的顏色
結(jié)果如下

2、PCA分析
還是用上述數(shù)據(jù)
- 加載包
library(ggfortify)
library(cluster)
- 基礎(chǔ)圖形
autoplot(object = prcomp(data), data = data)
#參數(shù)解析
# autoplot函數(shù)是ggplot包所屬的一個(gè)函數(shù)??梢詫?shí)現(xiàn)基于特殊對(duì)象實(shí)現(xiàn)快速繪圖。
# object指定繪圖對(duì)象
# data指定繪圖數(shù)據(jù)集
# prcomp函數(shù)基于給定數(shù)據(jù)集進(jìn)行PCA分析
# cleandata為進(jìn)行分析的數(shù)據(jù)集

- PCA聚類(lèi),用pam函數(shù)來(lái)代替prcomp函
autoplot(object = pam(data, 2), frame = TRUE, frame.type = 'norm')
#參數(shù)解析
# pam函數(shù)功能類(lèi)似prcomp函數(shù),區(qū)別在于它會(huì)將PCA的結(jié)果進(jìn)行K-means聚類(lèi)分析,在
# 函數(shù)的參數(shù)中,cleandata為數(shù)據(jù)集,3為K-means聚類(lèi)的K值,簡(jiǎn)單理解就是設(shè)置最終聚成幾類(lèi)。
# frame是一個(gè)邏輯值,設(shè)置是否在聚成一類(lèi)的數(shù)據(jù)點(diǎn)周?chē)砑油饪騺?lái)突出展示。
# frame.type設(shè)置添加的外框類(lèi)型,norm代表添加橢圓,默認(rèn)為多邊形

- 繼續(xù)修改圖片
#添加圖片標(biāo)題
autoplot(object = pam(data, 2), frame = TRUE, frame.type = 'norm') +
labs(title="PCA Analysis") +
#圖片標(biāo)題居中,設(shè)置字體大小為20
theme(plot.title = element_text(hjust = 0.5 ,size = 20),
#刪除圖例標(biāo)題
legend.title = element_blank()) +
#手動(dòng)指定圖列的顏色與名稱(chēng)
scale_fill_manual(values = c('red','blue'),labels = c('A','B')) +
scale_color_manual(values = c('red','blue'),labels = c('A','B'))

** 以上PCA分析內(nèi)容來(lái)自 R語(yǔ)言繪圖-PCA圖形優(yōu)化 **