I accidentally ran by your bookdown when I searched for how to display correlation matrix with hierarchical clustering tree. I noticed that your corrplot(correlationMatrix, order = 'hclust', addrect = 2) plot doesn't match with your pheatmap below in terms of variables' order and clustering. It's because in corrplot, the function takes the correlation matrix as a distance matrix and runs hclust directly on it. Meanwhile, pheatmap considers the correlation matrix as a normal data set and re-calculates the distance matrix before feeding it into hclust.
To make the two plots consistent with each other, I suggest changing pheatmap function to add two arguments (clustering_distance_rows and clustering_distance_cols) to it. It basically tells pheatmap to use the current correlation matrix as the distance matrix. The 1 - is to ensure that perfect positive correlation (1) is considered as min distance and perfect negative correlation (-1) is considered as max distance.
pheatmap(correlationMatrix,
clustering_distance_rows = as.dist(1 - correlationMatrix),
clustering_distance_cols = as.dist(1 - correlationMatrix))
I accidentally ran by your bookdown when I searched for how to display correlation matrix with hierarchical clustering tree. I noticed that your
corrplot(correlationMatrix, order = 'hclust', addrect = 2)plot doesn't match with yourpheatmapbelow in terms of variables' order and clustering. It's because incorrplot, the function takes the correlation matrix as a distance matrix and runs hclust directly on it. Meanwhile,pheatmapconsiders the correlation matrix as a normal data set and re-calculates the distance matrix before feeding it into hclust.To make the two plots consistent with each other, I suggest changing
pheatmapfunction to add two arguments (clustering_distance_rowsandclustering_distance_cols) to it. It basically tellspheatmapto use the current correlation matrix as the distance matrix. The1 -is to ensure that perfect positive correlation (1) is considered as min distance and perfect negative correlation (-1) is considered as max distance.