if(!require(som)){install.packages("som"); library(som)}
#[1]
ifname <- "DataBrandy.csv"
df<-read.delim(ifname,header=T,sep=",",row.names=1,as.is=TRUE,strip.white=FALSE)
ld<-rownames(df)
groupdata<-as.factor(substring(ld,1,2))
mdata<-scale(df)
ld; groupdata; head(mdata)
#[2]
xsize<-10; ysize<-10
brandy.som<-som(mdata, xdim=xsize, ydim=ysize,init="linear",alpha=0.5,
                alphaType="linear",neigh="gaussian")
#[3]------------------------
out.new<-data.frame(groupdata,brandy.som$visual[,1:2])
group<-unique(out.new[,1]); ng<-length(group)
for(i in 1:ng){
gdataset<-out.new[out.new[,1]==group[i],2:3]
namedata<-rownames(mdata[as.integer(rownames(gdataset)),])
ns<-dim(gdataset)[1]
rdata<-runif(ns,min=-0.4,max=0.4)
plot(gdataset[,1]+rdata,gdataset[,2]+rdata,col=i,pch=16,
     xlim=c(-0.5,xsize+1),ylim=c(-0.5,ysize-0.5),xlab="X",ylab="Y")
text(gdataset[,1]+rdata,gdataset[,2]+rdata-0.1,labels=namedata,cex=0.7)
par(new=T)
}
legend("topright", legend = group, col = 1:ng, pch = 16)
#[4]-------------------------------------------------
for(i in 0:xsize){
  segments(-0.5, i-0.5, xsize-0.5, i-0.5)
}
for(i in 0:(ysize)){
  segments(i-0.5, -0.5, i-0.5, ysize-0.5)
}