R语言做PCA分析及可视化代码

setwd("/Users/apple/Desktop/R/")

data<-read.table("pcadata",header=T,row.names=1)

PCAdata.pr<-princomp(data,cor=TRUE)

summary(PCAdata.pr,loadings=TRUE)

predict(PCAdata.pr)

screeplot(PCAdata.pr,type="lines")

biplot(PCAdata.pr,choices=1:2,scale=1,pc.biplot=FALSE)

scoresdata=PCAdata.pr$scores

scoredatasz<-read.table("scoredatasz.txt",header=T,row.names=1)

library(scatterplot3d)

> with(scoredatasz, {

s3d <- scatterplot3d(Comp.1, Comp.2, Comp.3,        # x y and z axis

pch=19,        # circle color indicates no. of cylinders

scale.y=.75,                # scale y axis (reduce by 25%)

main="PCA analysis",

xlab="PCA1(47.12%)",

ylab="PCA2(16.65%)",

zlab="PCA3(16.03%)")

s3d.coords <- s3d$xyz.convert(Comp.1, Comp.2, Comp.3)

text(s3d.coords$x, s3d.coords$y,    # x and y coordinates

labels=row.names(scoredatasz),      # text to plot

pos=4, cex=.5)                  # shrink text 50% and place to right of points)

})

loaddatasz<-read.table("loadsz.txt",header=T,row.names=1)

with(loaddatasz, {

s3d <- scatterplot3d(Comp.1, Comp.2, Comp.3,        # x y and z axis

pch=19,        # circle color indicates no. of cylinders

scale.y=.75,                # scale y axis (reduce by 25%)

main="PCA analysis",

xlab="PCA1(32.44%)",

ylab="PCA2(22.94%)",

zlab="PCA3(12.83%)")

s3d.coords <- s3d$xyz.convert(Comp.1, Comp.2, Comp.3)

text(s3d.coords$x, s3d.coords$y,    # x and y coordinates

labels=row.names(loaddatasz),      # text to plot

pos=4, cex=.5)                  # shrink text 50% and place to right of points)

})

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容