Hello,
Below a modified function for displaying a correlation matrix by Vincent
Zoonekynd posted in his excellent tutorial. Compared to the matrix and the
legend, the colouring of the polygons is wrong. The error is in the
sequence: col=col[N*(mat[i,j]+1)/2].
I cannot seem to devise a sequence which will make the colouring consistent
with that in the legend. Can you help? The number of different colours is
given by the parameter "dens".
Thanks,
Serguei Kaniovski
#--FUNC. CORR. PLOT
cor.plot<-function(mat, dens, ticks, main="") {
n<-dim(mat)[1]
m<-dim(mat)[2]
N<-ifelse(dens>10,10,dens)
col=gray(1:N/N)
plot(NA, xlim=c(-0.1,1.3), ylim=c(-1,0.1), axes=0, xlab="",
ylab="",
main=main)
#POLYGONS
for (i in 1:n) {
for (j in 1:m) {
polygon(c((j-1)/m,(j-1)/m,j/m,j/m), -c((i-1)/m, i/m, i/m, (i-1)/m),
col=col[N*(mat[i,j]+1)/2])
}
}
#TICKS
text(seq(1/(2*n),1-1/(2*n),1/n), rep(0,n), toupper(substr(ticks,1,2)),
pos=3)
text(rep(0,m), seq(-1/(2*m),-1+1/(2*m),-1/m), toupper(substr(ticks,1,2)),
pos=2)
#LEGEND
for (i in 1:N) {
polygon(c(1.1,1.1,1.2,1.2), -c((i-1)/N, i/N, i/N, (i-1)/N),
col=col[N-i+1])
}
text(rep(1.2,N+1), -N:0/N, -(N/2):(N/2)/(N/2), pos=4)
}
options(digits=2)
#--NAMES
namjid<-c("mike", "tommy", "clara",
"silvio", "jimmy", "jeffrey", "carl",
"randy", "tara")
njid<-length(namjid)
#--CORRELATION MATRIX
corrmat<-diag(rep(0.5,njid))
corrmat[lower.tri(corrmat,diag=0)]<-runif(choose(njid,2),-1,1)
corrmat<-corrmat1+t(corrmat)
#--CALL
cor.plot(mat=corrmat, dens=4, ticks=namjid, main="")
[[alternative HTML version deleted]]