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)

})

你可能感兴趣的:(R做PCA分析及三维可视化)