#         Daniel Carr
#         Copyright 2005, 2006  class use permitted

colorMatView=function(data,correlation,dissim,colors,title='',rnd=2)
{

nclass = length(colors)-3  # number of color classes
                           # first 3 are black, white, gray 
nEdge = nclass+1

#1 Graphics setup
#2 Title
#3 Correlation matrix
#4 Data matrix 
#5 Case dissimilarity matrix
#6 Matrix Layout Key
#7 Color Keys 

#1 graphics setup_____________________________________
savePalette=palette(colors)

nr = nrow(data)
nc = ncol(data)
panels = panelLayout(nrow=2,ncol=2,  
      rowSize=c(nc,nr),rowSep=c(0,.10,0),
      colSize=c(nc,nr),colSep=c(0,.10,0),
      topMar=.8,leftMar=0,rightMar=0,
      bottomMar=1.3)  

cex = .9
cexTitle = 1.1

#2 Title__________________________________________

panelSelect(panels,mar='top')
panelScale()
if(length(title)> 1){
  	   text(.5,.9,title[1],cex=cexTitle)
      text(.5,.5,title[2],cex=cexTitle)
} else {
      text(.5,.8,title,cex=cexTitle)
}
#3 correlation matrix_________________________________

k=0.5
dx=c(-k,k,k,-k,NA)
dy=c(-k,-k,k,k,NA)

mat=expand.grid(list(x=1:nc,y=nc:1))
newx=rep(mat$x,rep(5,length(mat$x)))
newy=rep(mat$y,rep(5,length(mat$y)))

brksCor =seq(-1,1,length=nEdge)
polycol=cut(correlation,brksCor,include.lowest=T,labels=F)+3
rx=c(0.5,nc+.5)
ry=c(0.5,nc+.5)
panelSelect(panels,1,1)
panelScale(rx,ry)
polygon(newx+dx,newy+dy,density=-1,border=NA,col=polycol)
panelOutline()

#4 data matrix_________________________________

mat=expand.grid(list(x=1:nc,y=nr:1))
newx=rep(mat$x,rep(5,length(mat$x)))
newy=rep(mat$y,rep(5,length(mat$y)))

brksData =seq(min(data),max(data),length=nEdge)
polycol=cut(t(data),brksData,include.lowest=T,labels=F)+3
rx=c(0.5,nc+.5)
ry=c(0.5,nr+.5)
panelSelect(panels,2,1)
panelScale(rx,ry)
polygon(newx+dx,newy+dy,density=-1,border=NA,col=polycol)
panelOutline()

#4 case dissimilarity matrix__________________________

mat=expand.grid(list(x=1:nr,y=nr:1))
newx=rep(mat$x,rep(5,length(mat$x)))
newy=rep(mat$y,rep(5,length(mat$y)))

brksDissim=seq(0,max(dissim),length=nEdge)
polycol=cut(dissim,brksDissim,include.lowest=T,labels=F)+3
rx=c(0.5,112.5)
ry=c(0.5,112.5)
panelSelect(panels,2,2)
panelScale(rx,ry)
polygon(newx+dx,newy+dy,density=-1,border=NA,col=polycol)
panelOutline()

#5 Matrix layout key________________________________

panelSelect(panels,1,2)
scales = panelScale(inches=T)
panelFill(col=3)
text(.1,.4, 'Correlations',cex=cex,adj=0) 
text(.1,.15,'Data',cex=cex,adj=0)
text(mean(scales$rx),.15,'Dissimarities',cex=cex,adj=.5)
#text(mean(scales$rx),.4,'MATRICES',adj=.5)
mtext(side=3,"Matrices",cex = cexTitle)

#6 Color Key______________________________________________

panelSelect(panels,mar='bottom')
panelScale(inches=T)
  
xlocs = seq(1.5,7,length=nEdge)
xcen = (xlocs[-1]+xlocs[-nEdge])/2
newx= rep(xcen,rep(5,nclass))
newy= rep(.95,5*nclass)

polycol=c(4:(nclass+3))

hx = diff(xlocs[1:2])/2   # half x width
hy = .1                   # half y height
dx=c(-hx,hx,hx,-hx,NA)
dy=c(-hy,-hy,hy,hy,NA)

# hx below is to align left edge with center of text
polygon(newx+dx,newy+dy,density=-1,border=NA,col=polycol)
polygon(newx+dx,newy+dy,density=0,col=1)
brks = format(round(rbind(brksCor,brksData,brksDissim),rnd))

text(xlocs,rep(.7,nclass+1),brks[1,],cex=cex,adj=.5)
text(xlocs,rep(.4,nclass+1),brks[2,],cex=cex,adj=.5)
text(xlocs,rep(.1,nclass+1),brks[3,],cex=cex,adj=.5)

text(.1,.95,"COLOR KEY",cex=cex,adj=0)
text(.1,.7,"Correlation:",cex=cex,adj=0)
text(.1,.4,"Data:",cex=cex,adj=0)
text(.1,.1,"Dissimilarity:",cex=cex,adj=0)
palette(savePalette)
}
