0

I have a distance object

x <- matrix(rnorm(100), nrow=5)
d<-dist(x,diag=TRUE,upper=TRUE)

         1        2        3        4        5
1 0.000000 7.683422 8.210562 6.522327 6.091876
2 7.683422 0.000000 8.296771 6.641323 8.088297
3 8.210562 8.296771 0.000000 7.229307 6.133479
4 6.522327 6.641323 7.229307 0.000000 5.668197
5 6.091876 8.088297 6.133479 5.668197 0.000000

I want to create a heat map of only part of the distance matrix so I convert it to a matrix and subsection it i.e.

dm<-as.matrix(d)
dm[1:2,4:5]
         4        5
1 6.522327 6.091876
2 6.641323 8.088297

I now want to convert it back to a distance object so I can apply the following function to create a heat map. Any advice on how to either 1) convert the matrix back into a distance object so the function can handle it OR 2) adjust the function so that it create the heat.map without needing D to be a heat map. Thanks.

#
# coldiss()
# Color plots of a dissimilarity matrix, without and with ordering
#
# License: GPL-2 
# Author: Francois Gillet, August 2009
#

"coldiss" <- function(D, nc = 40, byrank = TRUE, diag = TRUE)
{
    require(gclus)

    if (max(D)>1) D <- D/max(D)

    if (byrank) {
        spe.color = dmat.color(1-D, cm.colors(nc))
    }
    else {
        spe.color = dmat.color(1-D, byrank=FALSE, cm.colors(nc))
    }

    spe.o = order.single(1-D)
    speo.color = spe.color[spe.o,spe.o]

    op = par(mfrow=c(1,2), pty="s")

    if (diag) {
        plotcolors(spe.color, rlabels=attributes(D)$Labels, 
            main="Dissimilarity Matrix", 
            dlabels=attributes(D)$Labels)
        plotcolors(speo.color, rlabels=attributes(D)$Labels[spe.o], 
            main="Ordered Dissimilarity Matrix", 
            dlabels=attributes(D)$Labels[spe.o])
    }
    else {
        plotcolors(spe.color, rlabels=attributes(D)$Labels, 
            main="Dissimilarity Matrix")
        plotcolors(speo.color, rlabels=attributes(D)$Labels[spe.o], 
            main="Ordered Dissimilarity Matrix")
    }

    par(op)
}

# Usage:
# coldiss(D = dissimilarity.matrix, nc = 4, byrank = TRUE, diag = FALSE)
# If D is not a dissimilarity matrix (max > 1), then D is divided by max(D)

# Example:
# coldiss(spe.dj, nc=9, byrank=F, diag=T)

# byrank= TRUE      equal-sized categories
# byrank= FALSE     equal-length intervals
4

0 回答 0