admin管理员组

文章数量:1123193

I am trying to figure out the way to assess similarity between a few vectors containing gene names. I have only gene names for this so I wanted to use Jaccard dissimilarity and then convert it it similarity and plot a heatmap of the results.

Here is an example of what I am trying:

library(vegan)
library(pheatmap)

data <- matrix(c(1, 0, 1,
                 1, 1, 0,
                 0, 1, 1,
                 1, 1, 1),
               ncol = 3, byrow = TRUE)

rownames(data) <- c("Gene1", "Gene2", "Gene3", "Gene4")
colnames(data) <- c("Sample1", "Sample2", "Sample3")
dissimilarity <- vegdist(data, method = "jaccard")
similarity <- 1 - dissimilarity

pheatmap(as.matrix(dissimilarity), main = "Jaccard Dissimilarity")
pheatmap(as.matrix(similarity), main = "Jaccard Similarity")                   

heatmap(as.matrix(similarity), main = "Jaccard Similarity")
heatmap(as.matrix(dissimilarity), main = "Jaccard Dissimilarity")

The code works fine for "dissimilarity" in which case diagonals are 0 as the samples are have no distance to themselves. However, converting to "similarity" I expect the diagonals to be 1 (because 1-0=1 and that the similarity between the sample and itself is full) but the plots show that diagonal is 0 for both "dissimilarity" and "similarity".

I have a feeling it is because the vegdist() function leaves some parts of the matrix empty and then both heatmap() and pheatmap() automatically fill those missing parts with 0s but I am not sure how to fix it without redoing the matrices resulting from vegdist().

Is my approach to calculate "similarity" from ""dissimilarity" wrong? How can I fix it and have correct values and colors in diagonals without manually changing the matrices that are plotted?

本文标签: rHeatmap diagonals generated from vegdist are wrong for similarityStack Overflow