if(!require(maptools)){install.packages("maptools"); library(maptools)}
ifname <- "DataBrandy.csv"
df<-read.delim(ifname,header=T,sep=",",row.names=1,as.is=TRUE,strip.white=FALSE)
#[1]-------------------------------------
PCAresults<-prcomp(df,scale=TRUE); names(PCAresults)
#[2]--- proportion & cum. variance -------- 
vardata<-PCAresults$sdev^2
totalv<-sum(vardata)
pr<-vardata/totalv
nf<-length(vardata)
names(pr)<-c(1:nf)
cumdev<-rep(0,nf)
cumdev[1]<-pr[1]                 
for (i in 2:ncol(df)) {              
  cumdev[i] <- cumdev[i-1]+pr[i]             
}
cumdev
#[3]----- Factorloadings---------------
scoreMatrix<-PCAresults$x
fdata<-cor(scoreMatrix,df)
factorloadings<-t(fdata)
##[4]score plot: Select 2 PCs ------
haxis<-1; vaxis<-2
#vaxis<-3
dataMatrix<-as.matrix(scoreMatrix[,c(haxis,vaxis)])
factorMatrix<-factorloadings[,c(haxis,vaxis)]
##[5] Plots of relative variance and cumalative variance
plot(c(1:ncol(df)),pr,main="Proportion")
plot(c(1:ncol(df)),cumdev,ylim=c(0,1),main="Cumulative Variance")
##[6]Score Plot ---
SP<-paste("Score Plot",haxis,"vs.",vaxis)
hlabel<-paste("PC",haxis,sep="")
vlabel<-paste("PC",vaxis,sep="")
plot(x=dataMatrix[,1], y=dataMatrix[,2],
     main=SP,xlab=hlabel,ylab=vlabel)
pointLabel(x=dataMatrix[,1], y=dataMatrix[,2],
           labels=rownames(dataMatrix))
#[7] Loading Plot ---
LP<-paste("Lcore Plot",haxis,"vs.",vaxis)
plot(x=factorMatrix[,1], y=factorMatrix[,2],
     xlab=hlabel,ylab=vlabel,main="Loading Plot")
pointLabel(x=factorMatrix[,1], y=factorMatrix[,2],
           labels=rownames(factorMatrix))