0

I am trying to make a heat map from a distance matrix with diagonal 1. Actually, the distance matrix is cosine similarity. I plotted this matrix with fviz_dist in "factoextra" R package. I came up with this plot.

Cosine similarity matrix

As you can see, the diagonal indicates the value 0. But in cosine similarity, diagonal is 1 which means they are more similar. How can I change the value to 1 in the plot?

moonwalker7
  • 1,122
  • 3
  • 11
  • 29
Lzz0
  • 423
  • 1
  • 4
  • 13
  • *"How can I change the value to 1 in the plot?"* What do you mean? You already have a distance matrix with a 1 diagonal, don't you? Is this about how to plot a heatmap? Or how to calculate the cosine dissimilarity matrix? Either way, best to provide some sample data and the code you've tried so far. – Maurits Evers Feb 25 '18 at 11:17

1 Answers1

0

TBH, I'm unsure what you're after: whether this is about how to plot a heatmap? Or how to calculate a cosine dissimilarity matrix "by hand"? Either way, here is a worked-through example using some sample data.

For future postings, please learn how to ask good questions, and always provide a minimal reproducible example/attempt including sample data.

# Function to calculate cosine dissimilarity matrix
# Thanks to: https://stats.stackexchange.com/a/149865/121489
cos.sim <- function(ma, mb) {
    mat <- tcrossprod(ma, mb);
    t1 <- sqrt(apply(ma, 1, crossprod));
    t2 <- sqrt(apply(mb, 1, crossprod));
    return(mat / outer(t1, t2));
}

# Generate sample data
set.seed(2017);
x1 <- matrix(rnorm(45), ncol = 5);

# Calculate the cosine dissimilarity matrix
m <- cos.sim(x1, x1);

# Show heatmap
library(tidyverse);
m %>%
    as_tibble() %>%
    rownames_to_column("x1") %>%
    gather(x2, value, 2:10) %>%
    mutate(x2 = gsub("V", "", x2)) %>%
    ggplot(aes(x1, x2)) + geom_tile(aes(fill = value));

enter image description here

Note the 1 diagonal of m in the heatmap.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68