#[1]
ifname <- "irisAll.csv"
df <- read.delim(ifname,row.names=1,header=T,sep=",",as.is=TRUE,strip.white=FALSE)
dataMatrix<-df[,-c(5)]; head(dataMatrix)
classData<-df[,5]
labeldata <- as.factor(classData); head(labeldata)
PCAresults<-prcomp(dataMatrix,scale=T) 
#[2]--- Select two PC axes.
xselect<-1; yselect<-2
##yselect<-3
#------------ Select first 2 PCs ------
scoreMatrix<-PCAresults$x
dataMatrix<-as.matrix(scoreMatrix[,c(1,2)])
rownames(dataMatrix)<-labeldata; head(dataMatrix)
#[3]------------ 2D plot -----------------
xmax<-max(scoreMatrix[,xselect]); xmax<-as.integer(xmax); xmax<-xmax+1
xmin<-min(scoreMatrix[,xselect]); xmin<-as.integer(xmin); xmin<-xmin-1
# ------------------------------
ymax<-max(scoreMatrix[,yselect]); ymax<-as.integer(ymax); ymax<-ymax+1
ymin<-min(scoreMatrix[,yselect]); ymin<-as.integer(ymin); ymin<-ymin-1
#--  margin of x and y axes
xscale<-xmax-xmin; yscale<-ymax-ymin
usescale<-max(xscale,yscale)
xmax<-xmin+usescale; ymax<-ymin+usescale
print(xmin);print(xmax);print(ymin);print(ymax)
#[4]---- construction of groupdata ------
xgroup<-tapply(scoreMatrix[,xselect],labeldata,list)
ygroup<-tapply(scoreMatrix[,yselect],labeldata,list)
head(xgroup)
group<-levels(labeldata); ngroup<-length(group)
#[5] ---- Scatter plot on the screen or stored in the pdf.file ----
##pdf("PCABrandy1plot.pdf", paper="a4r", width=7, height=7, pointsize=10 )
par(ps = 10,mgp=c(1, 0, 0))
mainData<-paste0("Score plot between Z",xselect, " and Z", yselect)
haxisData<-paste0("Z",xselect); vaxisData<-paste0("Z",yselect)
n<-14+ngroup
pchdata<-c(15:n); pchdata[2]<-c(1)
plot(x=0,y=0, type="n", xlim=c(xmin,xmax),ylim=c(xmin,xmax),xlab=haxisData,
     ylab=vaxisData,xaxt="n",yaxt="n",main =mainData,pin=c(6,6))
for(i in 1:ngroup){
  points(xgroup[[i]],y=ygroup[[i]],col=1,pch=pchdata[i],cex=1.5)
}
axis(side=1,tck=0.01,at=seq(xmin,xmax,by=0.5))  
axis(side=2,tck=0.01,at=seq(ymin,ymax,by=0.5))
#n<-14+ngroup
#pchdata<-c(15:n)
legenddata<-c(group)
legend("topright",legend=legenddata,pch=pchdata,col=1,cex=1.0)
##dev.off()